I am trying to set up so that when I walk into a trigger, you press “e” key to enable an object and it’s not working.
What exactly is wrong with my code.
#pragma strict
var obj : GameObject;
function Start () {
obj.gameObject.SetActive(false);
}
function OnTriggerEnter (player : Collider)
{
//gameObject.SetActive(true);
if (Input.GetKey("e")){
obj.gameObject.SetActive (true);
Destroy(gameObject);
}
}
Try holding e while walking into the trigger and it will work. The problem is, that OnTriggerEnter is executed once and that’s when you need the key also down. If you want e to work as long as you “stay” inside the trigger, use OnTriggerStay and GetKeyDown. This way the code gets constantly executed and just listens for a key press. GetKey would just spam each frame the key is down, and that’s a lot during the time a user just presses the key, considering the high framerate.