Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 khaledadrani · Jun 08, 2019 at 12:25 PM · unity 5destroyparticlesystemeffectclone

How to destroy a clone of an effect attached to an object?

I want to use some particle effects into a bonus object that will be destroyed upon contact with the player object. This object as its name suggests will add a certain benefit for the player for a certain amount of time. I was following a tutorial on Youtube. The code from the Tutorial worked fine but there is one problem I was not able to solve: the explosion will not be destroyed after the object is collided with player and after the script is triggered because it seems that it was cloned into the scene. The instantiation created a clone of the effect's prefab I referenced into the script. After looking into the internet, I tried to affect the instantiation of the effect into a variable called clone and then destroyed it after the bonus is over but the problem was still present. I researched and I really did not obtain any answer.

bonus.cs //

     using System.Collections;
     using System.Collections.Generic;
     using UnityEngine;
     
     public class bonus : MonoBehaviour {
     
     public float multiplier=2.0f;
     public GameObject pickup_effect;
     public float duration = 10;
     private GameObject clone;
     
     private void OnTriggerEnter(Collider other)
     {
         if (other.CompareTag("Player")) StartCoroutine( pickup(other));
     }
     
     IEnumerator pickup(Collider player)
     {
         Debug.Log("You picked a bonus!");
         //spawn cool effect
         clone = Instantiate(pickup_effect, transform.position, transform.rotation);
         //apply effect yo the player
         // our.score_value += 10;
         player.transform.localScale *= multiplier;
         GetComponent<MeshRenderer>().enabled=false;
         GetComponent<Collider>().enabled = false;
     
     
         //wait x amount of seconds
     
         yield return new WaitForSeconds(duration);
     
     
         //remove the effect from theplayer
         player.transform.localScale /= multiplier;
     
         Destroy(gameObject);
         Destroy(clone,4f);
     
     
     }
 
 
 }

/

I tried disabling the "loop" option in the effect's particle system options but it gave the same results.

I disabled the "Play on Awake" option and the effect will not play at all.

The script is added directly to the object that is a simple 3D cube with MeshCollider, Mesh Renderer and Box collider.

The effect I used is a BigExplosionEffect taken from Unity Particle Pack 5.x from the Asset Store.

I would like to know what is exactly wrong? Is it probably related to the effect itself rather than the script? Or is there a far better solution?

Comment
Add comment
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

1 Reply

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

Answer by BradyIrv · Jun 08, 2019 at 05:35 PM

I think what you have is fine. You just need to call Destroy(clone,4) before you call Destroy(gameobject). Otherwise you are destroying the bonus object before the code to destroy the clone runs and therefore the clone will not be set to destroy after the four seconds.

Since it's a particle, you could also write some code right after instantiating the clone to destroy it after the duration of the particle effect (assuming it doesn't have child or chain effects that last longer than the main).

 // As quick example
 GameObject clone = Instantiate(effect, pos, rot);
 ParticleSystem.MainModule particle = clone.GetComponent<ParticleSystem>().main;
 Destroy(clone, particle.duration);
Comment
Add comment · Show 3 · 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 khaledadrani · Jun 10, 2019 at 05:29 AM 0
Share

I modified my code accordingly to your example. But it did not work. $$anonymous$$aybe it is due to the chain effects you mentioned? I think the effect lasts longer than the main because this is how it looks like after testing. I am not sure about it. $$anonymous$$aybe my modification was incorrect. Here is the new code:

//

  IEnumerator pickup(Collider player)
 
     {
         Debug.Log("You picked a bonus!");
         //spawn cool effect
         GameObject clone = Instantiate(pickup_effect, transform.position, transform.rotation);
         //apply effect yo the player
   
         player.transform.localScale *= multiplier;
         GetComponent<$$anonymous$$eshRenderer>().enabled=false;
         GetComponent<Collider>().enabled = false;
 
         // our.score_value += 10;
         //remove the effect from theplayer
        
         yield return new WaitForSeconds(duration);
 
         //wait x amount of seconds
 
         player.transform.localScale /= multiplier;
 
         ParticleSystem.$$anonymous$$ain$$anonymous$$odule particle = clone.GetComponent<ParticleSystem>().main;
         Destroy(clone, particle.duration);
         Destroy(gameObject);
 
     }
 
 
 }


//

I also made a screenshot so you can have an idea about what I am talking about. Is the problem related to the particle system itself? alt text I would like to understand what is going on in this matter because I feel it is the first real challenge I face in learning Unity. Thank You sincerely for your response.

avatar image BradyIrv khaledadrani · Jun 10, 2019 at 05:50 AM 0
Share

I'll have to take a look tomorrow in Unity. But, the code I mentioned should immediately follow the objects creation. Right now you have it running after a 10 second wait which defeats the purpose. They should be line for line ins$$anonymous$$d of yield retune wait inbetween.

avatar image khaledadrani BradyIrv · Jun 12, 2019 at 06:38 AM 0
Share

Thank you for the clarification. It worked.

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

209 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 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

destroy player cloned weapon (multiplayer 1 Answer

Unity Deleted My Game 1 Answer

How do I get a texture2D snapshot from a single rendered GameObject/SpriteRender/MeshRender in a scene? 0 Answers

Picture-in-Picture Effect 1 Answer

How can I destroy GameObjects on the screen with the same name? 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