var explosion : ParticleEmitter;
var smoke : ParticleEmitter;
function OnCollisionStay (collision : Collision) {
if (collision.gameObject) {
Instantiate(explosion, transform.position, transform.rotation);
Instantiate(smoke, transform.position, transform.rotation);
Destroy(gameObject);
yield WaitForSeconds (3);
explosion.emit = false;
}
}
Im trying to make it so that the particle effect explosion stops emitting or is destroyed 3 seconds after it is instantiated.
Can anyone give me a hint on what is wrong with this code?
Destroy(gameObject, 3.0);
Sorry in that code the line Destroy(gameObject); is meant for the enemy im destroying and when its destroyed the particles are instantiated then i needed “explosion” to go away after so many seconds.
Destroy(gameObject) will destroy the GO that the script is attached to.
Right, this script is attatched to the enemy NPC. Thats why i was trying to find a way to get the variable explosion to stop emitting at the end of the script.
Destroy(explosion.gameObject, 3); // and the same for smoke if you want to kill that
http://unity3d.com/support/documentation/ScriptReference/ParticleEmitter.html
Generally for things like this the scripting reference is very handy. If you scroll down to Inherited Variables you’ll see a list of components you can easily grab from a ParticleEmitter variable - gameObject included among them.
ah. i had searched through the reference. its how ive been writing everything so far. Must have just skipped over that.
hmm that is not working. Im getting the error “Destroying assets is not permitted to preserve data loss”
Destroy(explosion.transform.gameObject);
idk. explosion is still not going away, and still gettng the same error.
Well, you can just set explosion.enabled=false; instead of doing explosion.emit=false; I thought you were trying to destroy it.
jeez nothing seems to want to work.
When you Destroy(gameObject) it destroys everything attached to the GameObject, including scripts… so any code after the Destroy() line will never be run, because it doesn’t exist anymore.
You’d be better off putting a script on your explosion prefab that you are instantiating, and have it set emit to false on itself after 3 seconds.
ahh thanks legend it disappears now. I just have to figure out how to make it disappear after 3 seconds now. Using yield WaitForSeconds gives me a cannot use coroutine error. Appreciate the help.
you’re not killing it in the right place.
Make a script on the smoke and explosion that destroys them after 3 seconds, they’re already created on impact so the delay is the same - 3 seconds after impact. Only difference is that the program is going to destroy the right object.
edit:legend beat me to it lol
var explosion : ParticleEmitter;
//function Wait ()
function Update () {
if(explosion)
Destroy(explosion.particleEmitter.transform,3);
//particleEmitter.enabled = true;
//yield WaitForSeconds(3);
//particleEmitter.enabled = false;
//explosion.emit = true;
//yield WaitForSeconds (3);
//explosion.emit = false;
}
This is the new script i have attatched to explosion.
These are the things ive tried so far. It does disappear now but i cant get it to disappear after 3 seconds.
var explosion : ParticleEmitter;
function Start () {
if(explosion)
particleEmitter.enabled = true;
yield WaitForSeconds(3);
particleEmitter.emit = false;
}
Nevermind got it to work. I appreciate the help
The problem you had before was you cannot have a yield in function Update.
This should be the simpler way:
function Awake ()
{
Destroy(gameObject, 3.0);
}