- Home /
Making Sprite Disappear on Trigger
Hey guys so i have a sprite of a Cabin and I added a 2D box collider component to it with Is Triggered. I wrote a very small script (with no errors) that will destroy the GameObject when collided with. I also added a tag called "Cabin" which is referenced in the attached script. I have no idea why it doesnt work :( I messed around with the code a lot, switching Trigger with Collider, adding rigidbody to it and everything in between. This is like my sixth attempt at this and its really slowing me down. With Is Trigger on, he just walks right through it, with it off, he walks into it like a wall. Does anyone see what I did wrong, a setting I may have incorrect, or something in my little bit of code? Even if you have your own way of doing it in C# and would like to share, I would really appreciate it. Here is my code:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Cabin : MonoBehaviour {
public void OnTriggerEnter(Collider node)
{
if (node.gameObject.tag == "Cabin")
{
Destroy(node.gameObject);
}
}
}
and here is my Cabin in Inspector:
![alt text][1]
If you need to see anything else, I will be happy to upload it! Thanks so much! [1]: /storage/temp/97860-capture.jpg
Is this script attached to the player or to the object that has the trigger?
Answer by carle13 · Jul 17, 2017 at 09:32 PM
I saw the capture you uploaded, and it seems that the problem is that the script is attached to the cabin. The OnTriggerEnter doesn't work because a trigger can't detect when something has entered it, you need to check from the object that enters the trigger. So to fix it just attach the script to the player.
Alright man I'm going to give it a chance tomorrow and I'll let you know how it goes!
I tried it out and it worked! Thank you so much! I knew it was something simple but I feel a little dumb not trying that hahah
Answer by DeeCeptor · Jul 17, 2017 at 07:20 PM
OnTriggerEnter is for 3D collisions/triggers. You'll need to use OnTriggerEnter2D when using 2D colliders,
public class Cabin : MonoBehaviour {
public void OnTriggerEnter2D(Collider2D node)
{
if (node.gameObject.tag == "Cabin")
{
Destroy(node.gameObject);
}
}
}
@DeeCeptor I gave that a shot before and it didnt work again, player just walks right through the collider. Here is what I changed my code to:
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Cabin : $$anonymous$$onoBehaviour {
public void OnTriggerEnter2D(Collision node)
{
if (node.gameObject.tag == "Coin")
{
Destroy(node.gameObject);
}
}
}
I get the debugger error: Script error: OnTriggerEnter2D This message parameter has to be of type: Collider2D The message will be ignored.
I go back in my code where it says "(Collision node)" and change it to "(Collider 2D node)" and the error goes away.The player still walks through collider with nothing happening. Am I using the Collider2D in the wrong place?
The parameter needs to be Collider2D, not a Collision. View my updated answer.
Hmm, it still gives this error Script error: OnTriggerEnter This message parameter has to be of type: Collider The message will be ignored.