- Home /
Destroy Particle System in an Array
I'm created a particle system prefab used as forward thrusters on a space ship and had them instantiated into an array and follow the ship. I wanted the thrusters to be destroyed when I release the forward key.
The way I have it now does not work on destroying the Particle System. How do I destroy all the particle system within an array that stored in a variable in another function?
Below I have 2 functions one for starting the thrusters and another for destroying them. The starting function works perfectly when forward key is pressed.
using UnityEngine;
using System.Collections;
public class thrusterParticlesControl : MonoBehaviour {
public ParticleSystem[] forwardThrusters;
public GameObject[] forwardThrustersPos;
private float forwardInput;
private ParticleSystem[] instanceForwardThrusters;
private bool instanceForwardThrustersExists;
// Update is called once per frame
void Update ()
{
//Get user input
forwardInput = Input.GetAxis ("Vertical");
// If forward key is press, start thrusters, and destroy thrustsers([particle systems) when key is released
if(forwardInput > 0f)
{
startForwardThrusters();
}
else
{
stopForwardThrusters();
}
}
// Instantiate and store Particle system prefab is a variable.
void startForwardThrusters()
{
instanceForwardThrusters = new ParticleSystem[forwardThrusters.Length];
if (instanceForwardThrustersExists == false)
{
for (int i =0; i<forwardThrusters.Length; i++)
{
instanceForwardThrusters [i] = (ParticleSystem)Instantiate (forwardThrusters [i], forwardThrustersPos [i].transform.position, forwardThrustersPos [i].transform.rotation);
instanceForwardThrusters [i].transform.parent = forwardThrustersPos [i].transform;
instanceForwardThrustersExists = true;
}
}
}
// Destroy the instantiated prefabs from the startForwardThrusters function
void stopForwardThrusters()
{
if (instanceForwardThrustersExists == true)
{
for (int i = 0; i < forwardThrusters.Length; i++)
{
Destroy(instanceForwardThrusters[i]); // this instanceForwardThrusters[i] variable has nothing in it, or i'm doing this incorrectly
instanceForwardThrustersExists = false;
}
}
}
}
Quick question: Why are you instantiating and destroying particle systems ins$$anonymous$$d of just enabling and disabling (emission from) them?
I was following a tutorial for space game and this is how they did it. However they did only 1 particle system but I would like to do more then 1. I'll try turning them off and on ins$$anonymous$$d and see if that works. Well the question still stands, how would I reference the instantiated particle system in the startForwardThrusters in the stopForwardThrusters so I can turn them off?
Answer by Skymaster · Sep 12, 2014 at 01:38 PM
Thank you for the suggestion, it was a better option then creating and destroying particle systems.
This is what I used instead and attached the script to each particle system. It does what I want.
void startForwardThrusters()
{
ParticleSystem thrusters = (ParticleSystem)gameObject.GetComponent("ParticleSystem");
thrusters.enableEmission = true;
}
void stopForwardThrusters()
{
ParticleSystem thrusters = (ParticleSystem)gameObject.GetComponent("ParticleSystem");
thrusters.enableEmission = false;
}
Great that it worked!
I'd store the thrusters in a variable, and bind that with GetComponent on Awake() or Start(), that'll be nicer. You can also simplify your code by using Generics for the GetComponent:
ParticleSystem thrusters;
void Start() {
thrusters = gameObject.GetComponent<ParticleSystem>();
// other start stuff
}
void startForwardThrusters() {
thrusters.enableEmission = true;
}
void stopForwardThrusters() {
thrusters.enableEmisson = false;
}