system
April 6, 2011, 2:23pm
1
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!.
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!
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.