Instantiate problem.

I just want ONE object to be created at a spot but it just creates them endlessly, even made unity crash. :smiley:

using UnityEngine;
using System.Collections;
 
public class Spawning : MonoBehaviour {
	
    public GameObject target;
    Vector3 spawnSpot;
	bool once = false;
	

    void Start() {
		if(once = !once) {
	spawnSpot = target.transform.position;
    Instantiate(transform, spawnSpot, Quaternion.identity);
	once = true;
		} 
	}
}

Thanks in advance.

You are creating copies of yourself? Is that what you want to do? Make your once variable static:

static bool once = false;

As roberbu pointed out you’re creating copy of the object you have that script on. But then that object creates a copy of itself and so on and so on.

First question is: what are you trying to do? I.e. what’s your goal. There may be a better way.

As for fixing your script, I tested the following as working (creates one copy of object at the location of “target”):

using UnityEngine;
using System.Collections;
 
public class Spawning : MonoBehaviour {
 
    public GameObject target;
    Vector3 spawnSpot;
    static bool makeCopy = true;
 
 
    void Start() 
    {
       if(makeCopy) 
       {
          makeCopy = false;
          spawnSpot = target.transform.position;
    Instantiate(transform, spawnSpot, Quaternion.identity);
    
       } 
    }
}

Changing to static worked! My goal is to create a random number of enemies in random locations, but my idea was to start small to get the hang of things. I’m not sure I understand what changed when I switched the variable to static.