- Home /
What is wrong with my script
Spawn game object called polly, make it a child of the empty game object it spawned on. then when the bullet hits it it dies and another spawns.
The problem is that when the bullet hits polly another polly spawns INSTANTLY. I would like for the object to not be there for a few seconds then spawn. The script that handles the destroying and collision works fine.
Thanks
var polly : GameObject;
var chil = 0;
var alive : boolean = false;
function Update () {
chil = transform.childCount;
Check();
}
function Check(){
if(alive == false){
var ins : GameObject = Instantiate(polly, transform.position, transform.rotation);
ins.transform.parent = transform;
alive = true;
}
if(transform.childCount <= 0){
Debug.Log("dead");
alive = false;
}
}
Answer by Michael CMS · Nov 03, 2012 at 05:19 PM
You each frame check if the GameObject is alive, and if not you instantiate it. Your polly will be dead 1 frame, and alive the next.
What you want to do is set a timer to affect the delay .
Something like this :
...
var DelayTimeBetweenSpawns = 2.5;
var TimeSinceDeath =0.0;
..
function Check()
{
...
if (alive == false && TimeSinceDeath>DelayTimeBetweenSpawns)
{
// reinstantiate
}
else
{
TimeSinceDeath+=Time.deltaTime;
}
}
if (transform.childCount<=0 && alive )
{
alive=false;
TimeSinceDeath=0.0;
}
}
Please note that I didn't compile this and I code in C# , so some errors might slipped in.
Thank you so much. I have been trying to figure this out for 3 days. Worked perfectly, just add alive = true; after // reinstantiate and make the instantiated object a child of the current object
Your answer
Follow this Question
Related Questions
Teleporting script won't work 2 Answers
Choosing random function 3 Answers
Call a function multiple times 2 Answers
Object wont object get destroyed 1 Answer