- Home /
OnCollisionExit not being called
Hi, I'm making a game which point is to get water from start to end by using ways on hexagons. When childs of two hexagons (inputs and outputs for water) collide everything works fine but when I destroy one hexagon, method OnCollisionExit in not being called. I use OnCollisionStay so I know that collision happened and its not printing any collision with any other object when I destroy every object with what can it collide. Here is code:
Child (Collider):
public class Child : MonoBehaviour {
public int waterChild = 0;
public int sleep = 1;
public bool main = false;
private GameObject collis;
private void OnCollisionStay(Collision col) {
if (col.gameObject.name == "Collider" || col.gameObject.name == "Start" || col.gameObject.name == "End")
{
collis = col.gameObject;
if (col.gameObject.name == "Collider")
{
if (gameObject.transform.parent.GetComponent<Parent>().waterParent == 1)
{
col.gameObject.transform.GetComponent<Child>().waterChild = 1;
col.gameObject.transform.GetComponent<Child>().main = true;
Debug.Log("'Child of " + this.gameObject.transform.parent.name + "' Sending WareChild 1 to " + col.gameObject.name + " of " + col.gameObject.transform.parent.name);
}
}
if (col.gameObject.name == "End")
{
if (gameObject.transform.parent.GetComponent<Parent>().waterParent == 1)
{
col.gameObject.transform.GetComponent<End>().waterEnd = 1;
}
}
}
Debug.Log("'Child of " + this.gameObject.transform.parent.name + "' Collision with: " + col.gameObject.name);
if (collis.gameObject.transform.GetComponent<Child>().waterChild == 0 && main == true)
{
Debug.Log("'Child of " + this.gameObject.transform.parent.name + "' In" + collis.gameObject.name + " of " + collis.gameObject.transform.parent.name + " WaterChild is 0");
waterChild = 0;
}
}
void OnCollisionExit(Collision collision)
{
waterChild = 0;
Debug.Log("'Child of " + this.gameObject.transform.parent.name + "' Collision was stopped");
}
}
Do you have any ideas where can be problem or any other solution ?
Hello @Rinkimirikata my first suggestion would be to check if rigidbody is attached. Another problem may be that if you will destroy the object, its script is also destroyed and OnCollisionExit will never get called.
I would suggest using the different system to deter$$anonymous$$e collisions. For Ex. List ob neighbour objects, raycast, hexagonal field manager...