Destroy smoke after it's gone?

You dont have to use two scripts, just this last one above, the other script was a test that I was doing to check if the timescale affects the destroy timer

kk, just seen what issue I’ve been having. Hmm…this will be interesting to try out.

1 Like

It is a fun mechanic indeed

Going to finish building the main game and then have an attempt at a few other ideas to alter the game in interesting ways.

2 Likes

This, but I prefer to read the time from the duration of the particle system:

	// Use this for initialization
void Start () {
    Destroy(gameObject, gameObject.GetComponent<ParticleSystem>().duration);
}
3 Likes

@Gianni_Donadeo
Even better this way! :clap:

very nice. I had no idea you could set a duration value there.

The line by Gianni should be corrected like this:

Destroy (gameObject, gameObject.GetComponent<ParticleSystem>().duration + gameObject.GetComponent<ParticleSystem>().startLifetime);

or, ofc, like this for better readibility:

ParticleSystem timer = gameObject.GetComponent<ParticleSystem>(); Destroy (gameObject, timer.duration + timer.startLifetime);

This is because we need to destroy the object after the last particle has been killed, which happens after a (duration + lifetime) time.

6 Likes

What would have to be done to incorporate this directly into the brick script instead of using another script on the particle system?

I tried adding these 2 lines of code to the SmokePuff method but received the unity error:
MissingComponentException: There is no ‘ParticleSystem’ attached to the “1 Hit Brick (11)” game object, but a script is trying to access it.

Which makes sense as we we don’t have a particle system on the brick directly. However as we are instantiating and effecting the colour in this method, cant we also use the duration timers and destroy somehow?

Oh and im using unity 5 because i love making things difficult for myself lol

Hello @Sam_Blank,
sure you can! would you mind sharing the code when the particle gets instantiated?
It would be something like this:

GameObject ParticleName = Instantiate(etc..etc..etc..);
ParticleSystem partComp = ParticleName.GetComponent<ParticleSystem>();
Destroy(ParticleName, partComp.duration + partComp.startLifetime);
1 Like

My Smokepuff method for unity 5 is currently:

void SmokePuff () {
GameObject SmokePuff = Instantiate(Smoke, gameObject.transform.position,Quaternion.Euler(0,180f,0));
SmokePuff.GetComponent<ParticleSystem>();
ParticleSystem.MainModule main = SmokePuff.GetComponent<ParticleSystem>().main;
main.startColor = gameObject.GetComponent<SpriteRenderer>().color;
}

Hi, this is more complicated than it has to be.

All you need is:

GameObject SmokePuff = Instantiate(Smoke, gameObject.transform.position,Quaternion.Euler(0,180f,0));
Destroy (SmokePuff, time);

With “time” being the variable for how long you want it to be destroyed after.

1 Like

Haha yes that does look much simpler. I take it time is a public float?

If you wanted to use the “timer.duration + timer.startLifetime” from Galandil’s code though, how would that be incorporated?

this should work:

[code]
void SmokePuff () {
GameObject SmokePuff = Instantiate(Smoke, gameObject.transform.position,Quaternion.Euler(0,180f,0));
ParticleSystem partComp = SmokePuff.GetComponent();
ParticleSystem.MainModule main = SmokePuff.GetComponent().main;
main.startColor = gameObject.GetComponent().color;
Destroy(SmokePuff, partComp.duration + partComp.startLifetime);

}[/code]

the problem using the “time” value, is that you are creating a variable to simulate an value that already exists as @Galandil pointed, thus is one more variable to create problems, to cash in the memory and to be aware of.

1 Like

This is great! works like a charm :grinning:
Really appreciate you taking the time out to reply!

I got a couple of cautionary warnings about deprecated properties in visual studio. It recommend i change it to:

Destroy(SmokePuff, partComp.main.duration + partComp.main.startLifetimeMultiplier);

Do you know of a way to make visual studio automatically change these things for you?

1 Like

I’m glad to know that it worked!

Unfortunately I don’t know how to do that, sorry :frowning:

1 Like

No worries :slight_smile:

I know when you open an old project in a later version of unity it does the changes for you and obviously visual studio knows the changes that need to be made. If i find a solution i will be sure to post for other students.

1 Like

I would be happy to know how to do it too!

1 Like

Update: Actually the only thing that’s necessary to destroy the object at the end of its duration is: Destroy(smokePuff, main.duration); at the end of the smokePuff function.

In order to get the Particle System’s duration and have the code destroy it at the end of the duration in Unity 201.1.0p4 the code has to be changed a bit.

Here is the necessary puffSmoke code for Unity version 2017.1.0p4:

void puffSmoke(){
GameObject smokePuff = Instantiate(smoke, transform.position, Quaternion.identity) as GameObject;
ParticleSystem.MainModule partComp = smokePuff.GetComponent().main;
ParticleSystem.MainModule main = smokePuff.GetComponent().main;
main.startColor = gameObject.GetComponent().color;
Destroy(smokePuff, partComp.duration);
}

Nice code example Brady, thanks for sharing :slight_smile:

You could shorten it a little further by not calling the GetComponent() method twice, you’d get a teeny tiny performance boost too.

void puffSmoke(){
    GameObject smokePuff = Instantiate(smoke, transform.position, Quaternion.identity) as GameObject;
    ParticleSystem.MainModule main = smokePuff.GetComponent().main;
    main.startColor = gameObject.GetComponent().color;
    Destroy(smokePuff, main.duration);
}

Note, the use of GetComponent(), accordingly to the documentation, requires the specification of a Type. I wonder whether the above works without specifying one because perhaps the game object only has one component and just returns that, or, runs through all of the components and finds one with the corresponding properties (color / duration), the latter would obviously be worse.

I would always specify a type when using GetComponent(), and if you check the documentation on the link below using a Type, rather than a string, should be more efficient also.

Hope this is of use. :slight_smile:


See also;

Privacy & Terms