- Home /
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);
Answer by zombience · Nov 12, 2013 at 07:53 PM
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.
Your answer
Follow this Question
Related Questions
How to add an asset to a script-enabled public game object? 1 Answer
GameObject change Position after game started 1 Answer
prefab is instantiating without a script 2 Answers
Is it possible to disable/enable game-object inside Instantiate PreFab c# 0 Answers
PlayerRespawn class wont Instantiate the player prefab 1 Answer