Instantiation Once (yet again)

New to Unity, so be gentle. Started today.

I am putting together a very basic environment-builder. I have an interface up and running with panels and icons that I want to press to spawn different Prefabs. I’ve gotten my prefab to spawn via button press with this very basic script:

var instance : GameObject;

        function Start () {
        		
        		Instantiate(Resources.Load("Building_A", GameObject));
        		}

I’ve been hunting through Google, Unity documentation, and this site for a very simple way to stop Prefabs from instancing infinitely. I have seen some very confusing examples involving booleans, setting the max number of Prefabs to 1 and doing a check, cancel invoke, and a few others.

Several solutions simply say not to use use Function Update-(so it only spawns one instance) which I don’t believe I am using. Do I not have this running under Function Start as above?

Is there not a very simple way to do this? If you can spawn a prefab in one line of code is there not a way to cancel the instantiation as simply? Do you have to use Destroy or the like?

Bear in mind I have zero programming/scripting experience. RTFM responses are welcome provided someone can point me to where it explains this simply for a neophyte. I can’t seem to find a clear example with simple code like mine above.

Any help appreciated.

I’m not sure i understand what you want to do 100%.

but if you want to create an a prefab on a button press, don’t use the “Start” function.
create a new function, that is called only from the button, and use that for the instantiating.

if it HAS to be in the Start() function, you can make sure the class (script) is a Singleton (read more about it to know how to implement).

a Singleton is basically a class that can only have one single instance in your whole application.

Hi,

The Start function is read only once: when the gameobject that contains the script is loaded. So you don’t want to use this function to instantiate through a UI button.

What you need is the following: an Canvas and a button, a little script that you will call via the button and that will do the job for you and of course, a prefab.

Create a canvas and then a button, call it, design it whatever you like.
Create a script (C# or JS), you can put it anywhere in your scene but you should put it on the canvas so you know that all your UI-related scripts are here and you can find them easily.
In this script you will want to write a public function (so you can access it from outside of the script) that will instantiate your prefab.

JS:

public function InstantiateMyGameObject () {
      // Arguments are: prefab name + position where to instantiate + rotation
     Instantiate(Resources.Load("prefabName"),Vector3(15,106,212),Quaternion.Identity);
}

C#:

public void InstantiateMyGameObject () {
     Instantiate(Resources.Load("prefabName"),new Vector3(15,106,212),Quaternion.Identity);
}

Now we’re almost done, go on your button, find the component button and at its bottom you should find a “OnClick()” zone, add an event by clicking on the little +. Now in the little input field that appeared drag and drop your canvas or whatever object you put your script on it. Then click on the “No function” drop down menu that became interactable, find the name of your script and select the name of your public function.

That’s it ! You can run the game and click on your button, 1 click = 1 prefab. Because they are at the same position you will see only one but you can see them all in the hierarchy view.

If I misunderstood your question and that you wanted only to know how to instantiate only 1 prefab despite the number of clicks on the button, juste let me know !

Cheers