Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by mattcaius00 · Oct 01, 2018 at 12:51 AM · instantiateprefabparticlesdestroydestroy object

How to Destroy Particle System after Instantiating,How to destroy a particle system that was just spawned?

I've tried Destroy() but it doesnt sem to work,I just instantiated a particle system, which I want to destroy. I've tried Destroy(Particle_System); doesn't work.

 void Awake()
 {
    transformleftEngine = LeftEngine.GetComponent<Transform>();
    transformrightEngine = RightEngine.GetComponent<Transform>();
 }
 // Update is called once per frame
 void Update () {

     float scalarProduct = Vector2.Dot(transform.up, rb.velocity);
     float forceFactor = -2 / Mathf.PI * Mathf.Atan(compressionFactor*scalarProduct) +1;
     Force = initialForce * forceFactor;


     if (Input.GetButtonDown("Engine"))
     {
         engines = true;
         Instantiate(engineFXright, transformrightEngine);
         Instantiate(engineFXleft, transformleftEngine);
     }
     if (Input.GetButtonUp("Engine"))
     {
         engines = false;

// This is where I want to destroy the particles

     }
Comment
Add comment · Show 1
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 ifurkend · Oct 01, 2018 at 01:08 AM 0
Share

When you instantiate something, a clone is created to your scene, hence you need a Transform variable in your script to represent that clone if you want to manipulate it further such as destroying it.

But I need to also stress that instantiate a prefab then destroy its clone during Update risks memory leak. I assume you would constantly playing the same particle effect in thr same scene. If that’s the case you should ins$$anonymous$$d instantiate the prefab during Awake/Start/OnEnable to a Transform var and GetComponent of the particle system to a ParticleSystem var (uncheck “Play On Awake” of your particle effect prefab). During Update, clear and (re)play the particle system variable.

1 Reply

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

Answer by Trevdevs · Oct 01, 2018 at 01:04 AM

You need to add a reference, unity cannot just find the engineFXleft and right when you instantiate them. If you want to do it your way add references such as this.

 // Above awake
 
 private GameObject leftFX;
 private GameObject rightFX;
 
 //Change the following
 
 rightFX = Instantiate(engineFXright, transformrightEngine) as GameObject;
 leftFX = Instantiate(engineFXleft, transformleftEngine) as GameObject;
 
 //Where you want to destroy
 
 Destroy(leftFX);
 Destroy(rightFX);


Personally, I don't see why you just stop the particle system and reactive it when you need it instead of instantiating over and over again which is tasking on the computer. If you want to try it this way follow this code

  //Assign in inspector
     public ParticleSystem leftFX;
     public ParticleSystem rightFX;
 
     void Awake()
     {
         transformleftEngine = LeftEngine.GetComponent<Transform>();
         transformrightEngine = RightEngine.GetComponent<Transform>();
     }
     // Update is called once per frame
     void Update()
     {
         float scalarProduct = Vector2.Dot(transform.up, rb.velocity);
         float forceFactor = -2 / Mathf.PI * Mathf.Atan(compressionFactor * scalarProduct) + 1;
         Force = initialForce * forceFactor;
         if (Input.GetButtonDown("Engine"))
         {
             engines = true;
             //Make sure they are not on play on awake
             leftFX.Play();
             rightFX.Play();
         }
         if (Input.GetButtonUp("Engine"))
         {
             engines = false;
             leftFX.Stop();
             rightFX.Stop();
         }
     }


I didn't test this as im not home but it worked in my head let me know if it doesn't :)

Comment
Add comment · Show 1 · 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 mattcaius00 · Oct 01, 2018 at 03:23 AM 0
Share

Thanks!! It works perfectly, all I had to add was a stop() in the awake function to make sure the engines start off, apart from that it works perfectly.

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

138 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

help for start my project... 2 Answers

Destroying Objects. 1 Answer

how to instantiate object with content prefab and not him self 2 Answers

Prefabs into array than delete them when user interacts 1 Answer

Prefab instantiation and destruction 3 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