Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Skymaster · Sep 12, 2014 at 01:21 AM · arraydestroyparticle system

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;
             }
         }
     }
 }
 

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Baste · Sep 12, 2014 at 09:06 AM 1
Share

Quick question: Why are you instantiating and destroying particle systems ins$$anonymous$$d of just enabling and disabling (emission from) them?

avatar image Skymaster · Sep 12, 2014 at 11:10 AM 0
Share

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?

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
 
 
     }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Baste · Sep 12, 2014 at 01:43 PM 0
Share

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;
 }

avatar image Skymaster · Sep 12, 2014 at 05:28 PM 0
Share

That also works, Thanks. I changed my code.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to turn object into a particle system and back? 0 Answers

How to destroy the first clone of a UI Image using an array every time I press a button? 1 Answer

How to destroy an instantiated object when I touch it? 0 Answers

Destroying Instantiated Prefab 1 Answer

How to destroy all the items with one function? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges