Object spawns too many times,Objects sometimes spawns too many times (solved thanks)

Hi. This sentence is unrelated but I wrote this twice now because I forgot to log in, so I’m gonna try to keep this short and to the point.


I’m making a block stacking game where you press a button or key to release an object and if it lands on a target platform, it spawns another of the same object, and if it doesn’t then the game restarts. If you land the second object on top of the first object, it still spawns a new one. This is all fine (coding might be total ass but it works at least).


The problem (at least this is what I think) is that both touching the objects and the platform spawns a new one, so if you touch both it spawns more than one, and if those land and touch them again, they just multiply until Unity explodes of fear.


I’ve included the code for the spawner (though it may not be enough. Also I know that there are a few problems in my code but for now everything except the part I mentioned works). I am a total newbie to all of this so if anything is unclear or stupid then sorry about that. I hope someone can solve this though. Thanks a lot!

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spawner : MonoBehaviour
{

	public GameObject[] Ladder;

	public void Start()
	{
		SpawnNewLadder();
	}

	public void SpawnNewLadder()
	{
		int i = Random.Range(0, 0);
		Instantiate(Ladder*, transform.position, Quaternion.identity);*
  • }*
    }

What i would do is to add a simple boolean into your objects script that you may call hasLanded or hasSpawned or whatever. Then you woud you it to spawn the object only once.

From what I see from the given code, I guess you have anothe script with OnCollisionEnter merhod attached to the object ? What I would do would look like that :

public class MyObject : MonoBehaviour
{
     private bool hasLanded;

     Start()
     {
          hasLanded = false;
     }

     OnCollisionEnter(Collision other)
     {
          if(!hasLanded)
          {
               //check if you landed were you wanted or not

               //spawn your object

               hasLanded = true;
          }
     }
}