Heya Unity Answers! This script is driving me crazy. I want the game to spawn one character at the start, wait 3 seconds, destroy that character and replace it with another. Then wait 3 seconds again, destroy, spawn, wait, destroy, spawn… you get the idea.
No matter how much I play with the code however, something is always wrong. Right now the first time it works, the character gets deleted and then a new one spawns… aaaaand then new characters just spawn after 3 seconds without the old ones being deleted. What should I do?
#pragma strict
var AllCharacters : Character[];
static var activeChar : boolean;
var charSpawned = false;
var timeChar = 3.0f;
function Start () {
activeChar = false;
}
function Update () {
if(activeChar == false && charSpawned == false) {
var randChar = Random.Range(0, AllCharacters.Length);
Debug.Log (randChar);
activeChar = true;
}
if(activeChar == true && charSpawned == false) {
var x : int;
x = randChar;
Instantiate(AllCharacters[x].character, transform.position, transform.rotation);
charSpawned = true;
timeChar = 3.0f;
}
if(charSpawned == true) {
timeChar -= Time.deltaTime;
if(timeChar < 0f)
{
Destroy (GameObject.FindGameObjectWithTag("Character"));
charSpawned = false;
activeChar = false;
}
}
}
Thanks in advance!