I am trying to detect triggers between different objects. I do have a Rigidbody2D attached to all of the objects that are supposed to collide. The collision is detected if the enemy hits the bullet, but not if it hits the planet. here is my code: private void OnTriggerEnter2D(Collider2D collision) { if (collision.CompareTag("Laser")) { spawner.score = spawner.score + 1; Instantiate(particle, transform.position, transform.rotation); Destroy(gameObject); }else if (collision.CompareTag("planet")) { spawner.planethealth = spawner.planethealth - 1; Destroy(gameObject); } }
1) use code tags 2) use Debug.Log to see if the issue is the method is not called or the if statement doesn't execute 3) try "Planet" instead of "planet", remember, it's case sensitive, compute doesn't know about lower/upper case, it's different stuff as far as his concerned.
In the comment toolbar kajiiger, theres a page that has '<>' on it next the the words 'code:' Just click it and put the code there
Ok thanks. I checked and it is the else if statement that is not being called. how do I fix this? Code (CSharp): private void OnTriggerEnter2D(Collider2D collision) { Debug.Log("hit"); if (collision.CompareTag("Laser")) { spawner.score = spawner.score + 1; Instantiate(particle, transform.position, transform.rotation); Destroy(gameObject); }else if (collision.CompareTag("planet")) { //This part not working Debug.Log("Planet hit");//I never got this message in the console spawner.planethealth = spawner.planethealth - 1; Destroy(gameObject); } }