Spawn Prefabs

I want to spawn a certain number of prefabs * a variable. This is some of my script so far:

var SpiderAmount = 0;
var DeadSpider = false;
var gameStart = true;
var instObj : GameObject;

function Update () 
{
	if (gameStart)
	{
		SpiderAmount += 1;
		
		instObj = Instantiate ("CELSPDR", Vector3 (2.0, 0, 0), Quaternion.identity);

1 Answer

1

put it in the start function, which is a built in unity function that will get called either when you start the scene.

apologies if my for loop isn’t quite right, but i don’t javascript much.
but anyway, you should make a for loop in the start function.

function Start()
{
   for(var i:int = 0; i < SpiderAmount; i++)
   {
        Instantiate ("CELSPDR", Vector3 (2.0, 0, 0), Quaternion.identity);
   }
}

I get this error for the Instantiate line: No appropriate version of 'UnityEngine.Object.Instantiate' for the argument list '(String, UnityEngine.Vector3, UnityEngine.Quaternion)' was found. I don't know what it means and how to fix it. Do you?

oh, you should replace the string "CELSPDR" with an actual prefab reference. like this: var spiderPrefab:GameObject; function Start() { for(var i:int = 0; i < SpiderAmount; i++) { Instantiate (spiderPrefab, Vector3 (2.0, 0, 0), Quaternion.identity); } } Unity doesn't know what you mean by a string there. it needs an actual GameObject. create your spider prefab and link it to this script in the inspector by dragging it from your project window into the inspector window.

Thanks!!!!