- Home /
How to make the clones of a zombie continue to clone?
Basically I have a scrip to spawn zombies. I would like them to spawn in waves, but that is another story. My issue lies with the main zombie prfab. Whenever I run the game and I kill that first zombie. It no longer will make zombies. How do I fix it?
pragma strict
-----variables start--------
var zombie : Transform;
var clone : Transform;
var zombiesSpawned : int;
var zombies = 11;
var player : GameObject;
var spawn : boolean;
var min : int;
var max : int;
var wait : int;
var spawnPoints : Transform;
var positions : Transform;
------variables end --------
// on start I set min and max, set spawn to true, and set a random number value
function Start ()
{
min = 1;
max = 10;
spawn = true;
wait = Random.Range(min, max);
}
// Update I have an if statement that calls the function Spawn
function Update ()
{
if(spawn)
{
Spawn();
}
}
// The function Spawn is where I believe my issue lies.
//This is where I make more zombies.
function Spawn()
{
if(zombie)
{
Instantiate(zombie, transform.position, transform.rotation);
zombies++;
NewWaitTime();
spawn = false;
SetSpawn();
}
else
{
Instantiate(zombie, transform.position, transform.rotation);
zombies++;
NewWaitTime();
spawn = false;
SetSpawn();
}
}
// a little extra if you can help why isn't it spawning more than one zombie?
function SetSpawn()
{
if(zombiesSpawned == 0)
{
zombiesSpawned = (zombies + 12);
}
yield WaitForSeconds(wait);
spawn = true;
}
function NewWaitTime()
{
wait = Random.Range(min, max);
}
You should use the code thingy. I don't know what to call it. Just click the button that says "101010", and paste your code into that. Don't paste it straight into the text box, because that's ugly. Here's an example:
Without code formatting:
function Update() { //Hi, exampleness. if(true) } //Do a thing } }
With code formatting:
function Update()
{
//Hi, exampleness.
if(true)
{
//Do a thing
}
}
I honestly didn't know how to do that. SO I tried to make it as neat as I could.
Answer by EnglishMuffin123 · Nov 26, 2013 at 06:18 AM
Well looking at your code it seems that your problem lies where you expected it. Your if statement says if(zombie) but zombie is a transform, not a boolean. That's probably throwing some errors for you. I'm not sure if the code will go on to the else case, but try fixing that and seeing what happens.
Well I get no errors. I get run time errors with it. Whenever I kill the prefab zombie it is copying it freaks out and says pretty much it can't find zombie. This may be due to the probelem its death script deletes the game object zombie?
If you wait before killing the zombie does it spawn another zombie? Also, your variable zombiesSpawned is an unassigned int. You're never going to make the if statement true since you're never giving it a value. You're only spawning one zombie because your instantiate only spawns one zombie. Your variable zombies acts as a counter but does nothing to add to the zombies being spawned. To do that try a for loop (if that's what you're trying to accomplish)
I didn't notice that about zombiesSpawned thank you. And if I don't kill the first zombie. They will spawn and continue to spawn. I did make sure to test that.
Ok, what have you placed this code on? An empty object or the zombie itself? Also, what's the var Clone for?
I was trying to make it copy the clone aka the var clone. And it is place on the zombie prefab itself.
Answer by rct3fan24 · Nov 26, 2013 at 07:12 AM
If you could somehow place the prefab zombie somewhere the player can't get to it, or vice versa, that would work. The prefab zombie would sit in it's place, while the scripts clone it into the play area. Not sure if this would work with your situation, but give it a shot...
I would do that but I am going to make it waves. When all the zombies die then the wave will end.