Unity editor is crashing OnTriggerEnter2D

Hi,

just trying to make a simple platformer where the next platform instantiates based on an array of locations set from the original platform’s location.
The original platform is a prefab containing the main platform with a collider so that the player can bounce off, a range of empty transforms to be used as a location to instantiate the next platform, and an empty that holds the box collider 2D to trigger the instantiation.

The code is in that empty

    public void OnTriggerEnter2D(Collider2D collider)
    {
        if (collider.gameObject.tag == "Player")
        {
            Debug.Log("Hit");
            int n = Random.Range(0, spawnPoints.Length);
            GameObject newPlat = Instantiate(platform, spawnPoints[n].transform, platform);
            Destroy(gameObject,2f);
        }
    }

“spawnPoints” is the array of transforms for possible positions for the next platform to be instantiated.

I’m pretty novice at coding, but I didn’t expect my code to outright crash Unity; is there anything that I’m doing here that might overwhelm the editor?
Thanks for any help, and sorry if I didn’t post enough info

Hello

I don’t exactly have an answer as to why this is crashing your editor but I do have an alternative solution if your willing to put time into into it.

Instead of Instantiating & destroying your platforms you should try activating and deactivating them instead using something called Object Pooling.
What this basically will do is for example spawn 20 platform prefabs in your hierarchy at run time and activate them in the scene when you need them, deactivate them when you don’t.

I use this method in all my games where I need to instantiate a lot of prefabs in my scene at once, it takes so much pressure off the CPU.

Here is a link to what im talking about link text