- Home /
Can't call GetKey inside OnTriggerEnter?
I want it so when I collide with the "stuff", the beeper will go off, and if I press "E" the beeper will go off and a black screen will appear. I have the beeper working (besides that it doesn't turn off when I press "E"), and I have a black screen but it doesn't work during my script, it's like I cant have the GetKey function in the code? I'm still learning and this is really confusing me, any explanation is much appreciated, here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Metal : MonoBehaviour
{
public GameObject sound;
public GameObject black;
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "stuff")
{
if (Input.GetKey(KeyCode.E))
{
black.SetActive(true);
sound.SetActive(false);
}
sound.SetActive(true);
} else{
sound.SetActive(false);
black.SetActive(false);
}
}
void OnTriggerExit(Collider other) {
if (other.gameObject.tag == "stuff")
{
sound.SetActive(false);
black.SetActive(false);
}
else
{
sound.SetActive(false);
black.SetActive(false);
}
}
}
Answer by Dragate · Dec 29, 2017 at 07:40 AM
OnTriggerEnter() will run once when trigger collision happens. It doesn't matter if you press E before or after trigger collision because method simply doesn't run at those frames. If you hold E pressed during collision, your code will work. If you want to enable E button functionality while inside the collider, you'll have to check for it in OnTriggerStay().
public GameObject sound;
public GameObject black;
void OnTriggerEnter(Collider other){
if (other.gameObject.tag == "stuff"){
sound.SetActive(true);
}else{
sound.SetActive(false);
black.SetActive(false);
}
}
void OnTriggerStay(Collider other){
if(Input.GetKeyDown(KeyCode.E) && other.gameObject.tag == "stuff"){
black.SetActive(true);
sound.SetActive(false);
}
}
void OnTriggerExit(Collider other){
sound.SetActive(false);
black.SetActive(false);
}
Answer by Kingofweirdos · Dec 29, 2017 at 05:45 AM
Have you tried input.getkeydown? Also you could try putting the key command first and then check whether the object is stuff or not. Have you tried a debug.log of the OnTriggerEnter to see if your collider is working or not. Also try to run a debug.log on the input command and also the find gameobject to test if it is actually finding the gameobject and responding to you pressing e/
I have it so when I collide, the sound plays, and what I want is to have it so when I collide, the sound plays AND IF i press e, the screen goes black (aka black.SetActive(true);). And I tired Get$$anonymous$$eyDown and no luck :(. I cant switch the two if statements because I want it to beep on collision, not only when I press $$anonymous$$ Any more tips? And thanks for the help!
Your answer
Follow this Question
Related Questions
Hello, How would one make a particlesystem activate triggers? (hopefully I worded that right) 0 Answers
OntriggerEnter condition with two colliders 2 Answers
On Trigger enters being called a frame late 1 Answer
How do I prevent multiple triggers using OnTriggerEnter? 1 Answer
OnTriggerEnter question 1 Answer