X
X
x_shelves_x2018-05-13 18:52:12
C++ / C#
x_shelves_x, 2018-05-13 18:52:12

Need help fixing Unity script in C#. Who understands?

Good day to all. The question is. You need to write a script, the main function of which is that when you enter the trigger object to which this script is attached, and when the player presses a key on the keyboard (for example, E), this trigger object is deleted. Here is my experience, but for some reason it doesn't work:

using UnityEngine;
using System.Collections;

public class radio : MonoBehaviour
{
    void OnTriggerEnter(Collider other)
    {
        Debug.Log("Trigger");
        if (Input.GetKey(KeyCode.E))
        {
            Destroy(gameObject);
            Debug.Log("Button");
        }
 
    }

The script responds to a trigger by outputting the word Trgger to the console, but does not respond to a click and does not display a Button message to the console.
**Input.GetKeyDown and Input.GetKeyUp** not working
Please help me
Thank you very much

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
Griboks, 2018-05-13
@x_shelves_x

void OnTriggerEnter(Collider other)fires at the moment of collision of trigger colliders. And pressing the keyboard key occurs at a different moment. It doesn't work, although not always. Usually such things are written in Update.

S
Suren, 2018-05-14
Jagaryan @saylar

You need OnTriggerStay

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question