I dont know if its because I use sprites, I want to make a prefab destroy itself when colliding with a sprite with tag "Barrier", they have Rigidbody2Ds, Box Collider 2Ds (with IsTrigger true) , same Z and they still dont collide. Can anybody identify what's wrong? This is my code if it helps. Code (CSharp): void OnCollisionEnter2D(Collision2D coll) { if (coll.gameObject.tag == "Barrier") { Destroy(gameObject); } }
check the tag (does the sprite really contain the tag, maybe spelling mistake). You could use Debug.Log("Test"); in OnCollisionEnter to check if there is a collision. I'm also new to unity, but I hope this will help!
No, thats not the problem :c, but thanx also the Debug.Log doesnt work, Ive tried, its not detecting the collision, may it be cause they are sprites?
Here is a screenshot of the Barrier's Components: http://prntscr.com/cfalet And here one of the projectile's: http://prntscr.com/cfam1h
Here's the whole projectile code: Code (CSharp): public class ShootScript : MonoBehaviour { public float MoveX; public float MoveY; public float MoveSpeed; Movement player; void Start() { player = GameObject.FindWithTag("Player").GetComponent<Movement>(); MoveX = player.FacingDirectionX; MoveY = player.FacingDirectionY; } void Update() { GetComponent<Rigidbody2D>().velocity = new Vector2(MoveX * MoveSpeed, MoveY * MoveSpeed); } void OnCollisionEnter2D(Collision2D coll) { if (coll.gameObject.tag == "Barrier") { Destroy(gameObject); } } }
There is no collision because both colliders are set as triggers. Try using OnTriggerEnter2D() instead-- you'll have to change coll from a Collision to a Collider.
So only the barrier should have IsTrigger and the could should be like this? Code (CSharp): void OnTriggerEnter2D(Collider2D coll) { if (coll.gameObject.tag == "Barrier") { Destroy(gameObject); } }