- Home /
Insantiate once only
Simply, how do you insantiate an object once when using a function OnCollisionEnter? So in this script, how would you only make it instantiate one firework? (By the way, the rest of the script works - I'm not missing the function or the variables, this is just the relevent part).
if (hit.gameObject.name == "Bottom") {
var instance : GameObject = Instantiate(fireworks, Vector3(2159.701, -178, 3312) , transform.rotation);
yield WaitForSeconds (5);
}
}
Thanks!.
Answer by AVividLight · Apr 06, 2011 at 04:16 PM
It would help to see the rest of your script, but, if you only want it to work ONCE and never again, you could do this:
var activated : boolean = false;
if (hit.gameObject.name == "Bottom" && activated == false) {
var instance : GameObject = Instantiate(fireworks, Vector3(2159.701, -178, 3312) , transform.rotation);
activated = true;
}
}
If you want it to work every few seconds, you could do this:
var activated : boolean = false; var timer : float = 3.0;
if (hit.gameObject.name == "Bottom" && activated == false) {
var instance : GameObject = Instantiate(fireworks, Vector3(2159.701, -178, 3312) , transform.rotation);
activated = true;
//As Long As This Is Not Update
yield.WaitForSeconds(timer);
activated = false;
}
}
If it is update, use this:
var activated : boolean = false; var timer : float = 3.0;
if (hit.gameObject.name == "Bottom" && activated == false) {
var instance : GameObject = Instantiate(fireworks, Vector3(2159.701, -178, 3312) , transform.rotation);
activated = true;
//If its not Update
TimerRun();
}
}
function TimerRun () { yield.WaitForSeconds(timer); activated = false; }
Just so you know, I haven't tried any of these codes... If you get any errors, let me know!
-- - Hope I Helped!
LoL... I know, but 1$$anonymous$$ is so much cooler than 995...
This is going to be annoying, but I sorted it myself. Just switched round the order of everything - I had my {} wrong. Anyway, I'll give you a tick because you tried hard! and I may end up using one of your examples.
Answer by e-bonneville · Apr 06, 2011 at 02:27 PM
function OnCollisionEnter(col : Collision) {
if (col.collider.CompareTag("Bottom")) {
var instance = Instantiate(fireworks, Vector3(2159.701, -178, 3312) , transform.rotation);
}
}
Please note that the above code is untested and may contain errors.
Is it giving you any errors? $$anonymous$$ake sure you have a collider tagged Bottom and that collision is actually occurring. You also need to have a variable named fireworks that contains a reference to a gameObject or prefab in order for this to work.