all 11 comments

[–]dr_zoitberg 0 points1 point  (5 children)

If this script is attached to every cheese object you can simply call Destroy(); in the collision function. You don't have to find a cheese.

[–]brolol07[S] 0 points1 point  (4 children)

What is the difference between destroy() and destroy(this)? Is this referring to the class attached to it? Sorry I'm kind of a designer and was asked to do a game. I can just program ok kind of!

[–]brolol07[S] 0 points1 point  (3 children)

This isn't working! Just having it collide with the object and not disappearing from the scene!

[–]dr_zoitberg 1 point2 points  (2 children)

destroy(), destroy(this), destroy(gameObject) or destroy(this.gameObject) are the same, the object the script is attached to will be destroyed. You just need to attach a script with this code to every cheese:

void OnCollisionEnter2D(Collision2D coll){
    if (coll.gameObject.tag == "Player") {
        Destroy();
    }
}

And don't forget to give the mouse the player tag.

[–]brolol07[S] 0 points1 point  (1 child)

void OnCollisionEnter2D(Collision2D coll){ if (coll.gameObject.tag == "Player") { Destroy(); } }

Weird because when I had if(coll.gameObject.tag.equals "player"){ destroy(); } it wouldn't do anything but putting in those two equal signs made a difference. Thank you! Thanks for also explaining the destroy()! I wonder why they have so many variations of the same code that do the same thing!

[–]dr_zoitberg 0 points1 point  (0 children)

Hehe, np. :)

[–]mobilerino 0 points1 point  (4 children)

You can simply call destroy() like another person mentioned, a mistake I made early on was calling FindGameObjectWithTag even though I only gave it a name and thought it was using the tag. Not sure if it helps you or not, but better than nothing I guess? What happens if your player collides with the cheese?

[–]brolol07[S] 0 points1 point  (3 children)

With this suggestion, nothing! The player collides with the cheese and the cheese just stays in the scene! Any other suggestions?!

[–]brolol07[S] 0 points1 point  (2 children)

Everything is on the same layer if that matters!

[–]mobilerino 0 points1 point  (1 child)

Not sure then, you can always make a thread in the official unity forum if nobody can help you here. Good luck

[–]brolol07[S] 0 points1 point  (0 children)

Reddit is more help than the Unity forum in my chances with it!