- Home /
Is their any way to save a particle system when it's not instantiated yet?
In my game obstacles have coins on them and the particle system for the coins are generated when the coins are randomly generated offscreen. Therefore when I try to refrence the particle system which triggers when the player runs into the coin I get an error because the particle system isn't in the inspector anymore?
How do I get around this?
Answer by c_Feng · Jan 05, 2019 at 11:52 PM
Are your coin objects being destroyed when the player runs into them?
If they are, then you need to uncouple the particle system from the coin game object and handle the particle system separately. One solution is to have a prefab with the desired particle system, and "Play On Awake" set to true. When the player runs into the coin, simply instantiate a copy of the prefab at the location of the coin. If you do this, make sure you destroy the instantiated game object when the particle finishes playing.
If your coin game object stays active in the scene, then you can have the particle system on the same game object, but with "Play On Awake" set to false so that the particle won't be generated with the coin. When the player runs into the coin, call ParticleSystem.Play() on a reference to the component to finally generate the particle.
Yes the coins are being destroyed when the player runs into them. I appreciate this because I really didn't know what to do. I'll get back to you when I run it