- Home /
Question by
smithr3339 · Dec 11, 2013 at 09:31 PM ·
collisiontopdownpickupcoin
Picking up a coin in 2D
So I am making a game in 2D mode. I was wondering how I could make it so when I walked through a coin it destroys it (just disappears) Can I have somebodies script? This is what I have. Sorry if its bad cause I'm still learning :P Also do I use a sphere or what?
function OnTriggerEnter(info : Collider) {
if (info.tag == "Player"){
Debug.Log("Debug");
}
}
Comment
Start from here, take a tour on tutorials of unity and try to understand the idea of it. Getting the exact script won't help you because you will have many other problems to struggle. Still, here is the code will do what you want:
function OnTriggerEnter2D(info : Collider2D)
{
if (info.tag == "Player")
{
Destroy(gameObject);
Debug.Log("You have gained X amount of golds!");
}
}
Add this script to your coin and tag your player as "Player".
Also make sure your collision box is checked as a trigger since you are using OnTriggerEnter and not OnCollisionEnter.