- Home /
OnTriggerExit2D Does not work
Hey guys, At first i want to appologys for my bad english, so my probleme is that the OnTriggerExit2D Function dont get called. using UnityEngine; using System.Collections; public class outofcam : MonoBehaviour { void OnTriggerExit2D(Collider2D colli) { if(colli.gameObject.tag == "outofcam") { Destroy(colli.transform.parent.gameObject); } if (colli.gameObject.tag == "cloud") { int newy = Random.Range(0, 6); colli.transform.position = new Vector2(transform.position.x + 50, newy); } } }
In my game there are some flying clouds which should go back if they go out of the bound which is a gameobject with the code above. So in my Level this works perfectly, but in the Mainmenu where I wanted to have the same effect this just wont work. I also did a prefab of the bound and the clouds which also didn't work. I'm really confused right now and don't know what i did wrong so please help me! Thanks!
Please format your code correctly. It's illegible at the moment.
Ohh, didn't saw it don't know why this got that format. I hope its better now sry thats my first post here.
Answer by Vcontrol · May 14, 2016 at 08:38 PM
You checked that you have Rigidbody2D component attached to you Cloud gameobject?
OnTriggerEnter2D/OnTriggerStay2D/OnTriggerExit2D won't get called, if there is no Rigidbody2D component.
Okey well worked for me thank you, but why did it worked in my first level, the clouds also have no Rigidbody2D ?
A Rigidbody2D component isn't required to get a trigger/collision callback.
When you don't add a Rigidbody2D component, the collider is static and two static colliders won't contact each other.