- Home /
Instantiate number of enemies
hi I'm trying to spawn more monster after 1 is defeated, so I wrote this script, but I couldn't figure it out what is wrong, or how to fix it. Please Help! I'm not originally a game programmer so I found this hard to do. and I want 3 number of objects to appear immediately after the first one is destroyed. that var childrenspawn : zombie = spawns[i] seems to give me error whatever I put there.
Thanks
var zombie : Transform;
function Update() {
var spawns = GameObject.Find("zombieSpawnPoint");
if(BiggerZombieAI.Eliminated)
{
for (var i=0;i<3;i++) {
var childrenspawn : zombie = spawns[i];
Instantiate(zombie, childrenspawn.transform.position, childrenspawn.transform.rotation);
}
Answer by Michael La Voie · Apr 07, 2010 at 07:30 PM
What is zombieSpawnPoint? GameObject.Find will only return one gameobject, but spawns[i] implies that it will return more than one.
If zombieSpawnPoint is simply a single location that you'd like to create three zombies at then try this:
var zombie : Transform;
function Update() {
var spawns = GameObject.Find("zombieSpawnPoint");
if(BiggerZombieAI.Eliminated)
{
for (var i=0;i<3;i++) {
Instantiate(zombie, spawns.transform.position,
spawns.transform.rotation);
}
}
Unfortunately, this code will create all three zombies at the same location, so you may want to randomize their spacing a bit.
Be aware that GameObject.Find shouldn't be placed in an Update() function due to speed. You can use this code for testing, but you should really find it once in your Start() function and save it.
Answer by Andrew Susanto · Apr 07, 2010 at 09:01 PM
I have found a way to do it, thanks for your answer without your answer I wouldnt be able to figure it out, although now I need to know how to make space out of them Thanks
var zombie : Transform;
function LateUpdate() {
var spawns = GameObject.Find("zombieSpawnPoint");
if(BiggerZombieAI.Eliminated)
{
for (var i=0;i<3;i++) {
Destroy(spawns.gameObject, i);
Instantiate(zombie, spawns.transform.position,
spawns.transform.rotation);
}
}
}
Your answer
Follow this Question
Related Questions
Checking if object intersects? 1 Answer
Projectile instantiation positioning is off. 3 Answers
Instantiate ground/enemies 1 Answer
Random instantiate script 1 Answer