- Home /
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?
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);
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? 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.
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.
Thank you for the clarification. It worked.
Your answer
Follow this Question
Related Questions
destroy player cloned weapon (multiplayer 1 Answer
Unity Deleted My Game 1 Answer
Picture-in-Picture Effect 1 Answer
How can I destroy GameObjects on the screen with the same name? 3 Answers