Double Spawn

I am using the below code to spawn a crashed item on collision. Sometimes, not always, I get a double spawn of the crashed vehicle. The player container is holding the normal vehicle.

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;

public class CrashedSleigh : MonoBehaviour
{
    public GameObject crashedSleigh;
    public GameObject playerContainer;

    void OnCollisionEnter(Collision collisioninfo)
    {
        if (collisioninfo.collider.tag == "Obstacle")
        {
            Instantiate(crashedSleigh, transform.position, transform.rotation);
            Destroy(playerContainer);
        }

       
    }
}

I think you can hit more than one collider in a given frame… is that it?

You can print out the name of the collider you hit at the start of the method… it is in collisioninfo

I will try that. Its very random, I was playing last night testing something else and hit one of my trees with a single collider and it spawned three. It was quite amusing but not what Im looking for.

And that’s why we’re all in gamedev.

One other obvious thought is: doublecheck you didn’t stick this in two places on your object, one that is on a collider that is harder to hit, that you didn’t originally intend it to be. I find physics collisions are almost always explainable once you start printing out the names of who was involved.

“Oh, I was hitting the monster’s left toenail… no wonder I died…”