How to destroy a looping particle effect that is part of an object?,How to destroy a particle effect attached to an object?
I have done a lot of looking up on this, but nothing seems to really fit what I'm trying to do (everyone is talking about a temporary effect like an explosion, or something that will last a duration, then end). My game is a 2D rogue-like, and I have pickups. I have created a little 8-bit "glow" particle effect and attached it to one of the pickup objects which is set to destroy on collision. The pickup itself works absolutely perfectly, no flaws at all. I have set both the pickup object and the particle effect to be a prefab and attached the effect to the pickup object as a child. So for obvious reasons, the particle effect needs to be set to "Loop" because I need it to continuously play until the object is picked up. In theory, when the object gets destroyed, the effect should as well since it's a child? Well, that's not happening...And I have even called to destroy the effect before the object and still nothing. The particle effect will continue on forever even after the gameobject is destroyed and I'm at a loss here. Find below my code for the pickup. Everything that is commented out are things that I have already tried before but did not work (just left them in there so you could see what I've tried).
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class HealthContainer : MonoBehaviour { //public ParticleSystem glowPartEffect; public GameObject glowEffect;
// Start is called before the first frame update
void Start()
{
//Instantiate(glowEffect, transform.position, transform.rotation);
}
// Update is called once per frame
void Update()
{
}
//public void OnBecameVisible()
//{
//Instantiate(glowEffect, transform.position, transform.rotation);
//}
//when the player collides with the pickup
public void OnTriggerEnter2D(Collider2D other)
{
if(other.tag == "Player")
{
PlayerHealthController.instance.AddHeartContainer();
Destroy(glowEffect);
//Destroy(this.gameObject.GetComponent<ParticleSystem>());
//DestroyImmediate(glowEffect);
//ParticleSystem.Destroy(glowPartEffect);
Destroy(gameObject);
AudioManager.instance.PlaySFX(25);
AudioManager.instance.PlaySFX(26);
}
}
//public void OnBecameInvisible()
//{
//Destroy(glowEffect);
//}
}
For the record, I have no problem instantiating the effect which happens when the pickup is dropped, and it instantiates properly and everything. Just can't seem to get rid of it once it's there. Haha.
Sooo... Shortly after posting this, I realized my mistake and finally got it to behave properly... I forgot to add the particle effect to the object before making it a prefab... So the effect was still a separate object and not following the script...