Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Game Development How to Make a Video Game Pickups Add Pickup Particles

aykutzdemir
aykutzdemir
8,655 Points

Maintainable Particles Duration

Is there a chance to use the duration of the particles directly from the particle system.duration instead of typing "5f" in the destroy method?

5 Answers

Nick Pettit
STAFF
Nick Pettit
Treehouse Teacher

It's possible, and it's probably OK on a small game like this for desktop devices, but it's not best practice. As the player collects more and more pickups, leaving particle systems there would clutter up the scene, which takes up memory.

This is particularly important on mobile devices. Imagine a space shooter for phones, where the player is destroying lots of asteroids and particles are flying everywhere. Leaving those particle systems in the scene would eat up way too much memory.

Cam Newton
Cam Newton
1,737 Points

I don't like hard coding timings (in case someone decides to tweak it in the future) so I looked into a more dynamic way of setting the destroy timer.

It uses the duration plus the number set in the startLifetime field so that it doesn't cut off the last spawned particle:

public class pickupParticlesDestruction : MonoBehaviour {

    private ParticleSystem ps;

    void Start () {
        ps = gameObject.GetComponent<ParticleSystem> ();

        /* Destroy the pickup particles, but 
           allow for the last spawned particle to die out first */
        GameObject.Destroy (gameObject, ps.duration + ps.startLifetime);
    }
}

dose it work ???

this happens when i do it

all compiler errors have to be fixed before you can enter playmode