This question was
closed Sep 12, 2015 at 07:20 PM by
Extrapafurdio for the following reason:
The question is answered, right answer was accepted
Question by
Extrapafurdio · Sep 12, 2015 at 10:40 AM ·
collider2dtriggersontriggerenter2d
void OnTriggerEnter() not working properly?
public bool testTrigger = false;
void onTriggerEnter2D(Collider2D key)
{
if (key.gameObject.CompareTag("Key"))
{
testTrigger = true;
key.gameObject.SetActive(false);
}
}
Simple as that, I just want to deactivate the key in the hierarchy. But, when the character enters the trigger, nothing happens. Not even the boolean appears as true in the inspector. This script is attached to the player game object, not to the key I want to deactivate. What is wrong if this code?
Comment
You're checking if the $$anonymous$$ey
gameObject enters the trigger. Shouldn't you check if (key.gameObject.CompareTag("Player"))
?
But that would set your Player deactive.
public bool testTrigger = false;
public GameObject key;
void Start(){
key = GameObject.FindGameObjectWithTag("$$anonymous$$ey");
}
void onTriggerEnter2D(Collider2D other)
{
if (other.gameObject.CompareTag("Player"))
{
testTrigger = true;
key.gameObject.SetActive(false);
}
}
I got it! I think I misunderstood how OnTriggerEnter works. Thank you very much! And I forgot the capital "o" when calling the method...