- Home /
Void OnTriggerEnter not working
I have a chest with a boxcollider2D that is set to: 'is trigger', and this script:
void OnTriggerEnter() {
if (Input.GetKey(KeyCode.UpArrow)) {
chest.SetBool ("open", true);
Also on the chest is an animator with a parameter called 'open', that when set to true plays the chest opening animation. But when my character walks in front of the chest and i press the 'up arrow', nothing happens! Please help!
Answer by tanoshimi · Aug 16, 2017 at 09:10 AM
"But when my character walks in front of the chest and i press the 'up arrow', nothing happens!"
That's because OnTriggerEnter fires in only the single frame in which you enter the trigger. You'd need to be holding dosn the up key, then walk in front of the chest for your code to work. Alternatively, use OnTriggerStay instead of OnTriggerEnter.
Thanks, i also realized that I was using OnTriggerEnter, rather than OnTriggerEnter2D.