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!
All I needed to do was change Update to FixedUpdate…
._.
Well at least it’s been resolved
Why not put a script on the object that destroys itself after 3 seconds.
Start()
{
Destroy (gameObject, 3);
}
Not too hot on JS so untested but in theory it should work, or put it in awake or reference each game object when instantiating like newCharacter = instantiate… and then the next line Destroy (newCharacter, 3);