Unity game freezes when three of the same game objects collide

Hi I have a problem with my OnCollision code which freezes the game engine when three of the same game objects collide. The code is designed to instantiate a new game object when two of the same game objects collide. It works great for this but when three of the same objects collides it freezes. I am stuck on how to resolve this. Can anyone help me with this? Thank you.

The code is;

void Start()
{
    ultraP = GetComponent<Rigidbody2D>();
    firstCollision = false;
}

void OnCollisionEnter2D(Collision2D other)
{
    UltraPumpkin sameCollide1 = other.collider.GetComponent<UltraPumpkin>();
    if (sameCollide1 != null)
    {
        if (other.gameObject.GetComponent<UltraPumpkin>().firstCollision == false)
        {
            firstCollision = true;
            Destroy(other.gameObject);
            MainEngine.instance.SameCollision(+16);
            Instantiate(nextPumpkin, transform.position, Quaternion.identity);
        }
        Destroy(gameObject);

    }


}

Most likely you have an infinite loop that isn’t shown in the code above.

What does that MainEngine.instance.SameCollision(+16); call do?

Probably just have to debug what is happening.

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

REMEMBER:

Unity will lock up 100% of the time EVERY millisecond your scripting code is running.

Nothing will render, no input will be processed, no Debug.Log() will come out, no GameObjects or transforms will appear to update.

Absolutely NOTHING will happen… until your code either:

  • returns from whatever function it is running

  • yields from whatever coroutine it is running

As long as your code is looping, Unity isn’t going to do even a single frame of change. Nothing.

No exceptions.

“Yield early, yield often, yield like your game depends on it… it does!” - Kurt Dekker

1 Like
void OnCollisionEnter2D(Collision2D other)
{
	if (firstCollision == false)
	{
		UltraPumpkin sameCollide1 = other.collider.GetComponent<UltraPumpkin>();
		if (sameCollide1 != null)
		{
			firstCollision=true;
			other.gameObject.GetComponent<UltraPumpkin>().firstCollision = true; // prevent the other object from being able to do stuff
			Destroy(other.gameObject);
			MainEngine.instance.SameCollision(+16);
			Instantiate(nextPumpkin, transform.position, Quaternion.identity);
			Destroy(gameObject);
		}
	}
}
1 Like

Thank you Kurt. The MainEngine code is for the score counter. I will debug and see if there are any issues with the code :slight_smile:

Thank you Zulo, did not think to include the null reference. I will give that a go. Most appreciated :slight_smile: