- Home /
Duplicate Question
Collide detection with tag [JS]
I dont why my script doesnt working but I am trying to do, if my my player collides with a object tagged with "block" it will call Kill function.
Here is the script:
var health : float=100;
function OnCollisionEnter2D (coll: Collision2D)
{
if(GameObject.FindWithTag("block"))
{
Kill();
}
}
function Kill()
{
Destroy(this.gameObject);
Application.LoadLevel("LoadedLevelName");
}
Your script "isn't working" (not that you've said what that means exactly) because your code logic is completely flawed.
Assu$$anonymous$$g that your OnCollisionEnter2D function is getting called at all (i.e. you've got the right setup of colliders, ridibodys etc.), then the next thing you do is to return an arbitrary gameobject from the scene tagged as "block":
GameObject.FindWithTag("block")
What you want to do is check whether the object *with which you collided" was tagged with "block" and this is covered in hundreds of tutorials as well as in the documentation. Please read it.
Follow this Question
Related Questions
How to make it so enemies only move towards the player when the player is colliding with an object 1 Answer
Player Destroy on Collision? 0 Answers
Unity 2D Colliders not Colliding 2 Answers
How to create random movement in 2D 2 Answers
can you have a Tag inside a tag and check for collision with that? 2 Answers