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?
Destroy(clone, 4f);
will never execute. This is because you destroy the pickup object before it can wait 4 seconds to destroy the clone. \$\endgroup\$