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 <em>= (ParticleSystem)Instantiate (forwardThrusters <em>, forwardThrustersPos _.transform.position, forwardThrustersPos *.transform.rotation);*_</em></em>
instanceForwardThrusters .transform.parent = forwardThrustersPos .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_); // this instanceForwardThrusters variable has nothing in it, or i’m doing this incorrectly
* instanceForwardThrustersExists = false;
}
}
}
}*_
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;
}