Having trouble destroying children instances.

I am writing a script that uses collision to trigger a particle emitter to be created at the center of object then play a looped particle emitter when another object collides with it. It’s spawning more than one instance of emitter and they are not dying? Any help is appreciated. I am still a noob.

Update…
I got it to work OnTriggerExit.
I used Destroy(Instantiate(Ping.gameObject, transform.position, transform.rotation), 5f);

Now I just need to figure out how to transform them to a new rotation value.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RadarPinger2 : MonoBehaviour
{
    //This script used in conjunction with RadarHit material.
    public GameObject Ping;
    public float frequency;
    Renderer rend;
      

    void OnTriggerEnter(Collider other)
    {
       
        bool enter = true;

        Debug.Log("OnTriggerEnter");



        if (enter == true)
            StartCoroutine(SpawnRadarPing());

        IEnumerator SpawnRadarPing()
        {
            while (true)
            {
                var ping = Instantiate(Ping, transform.position, transform.rotation, transform);
                ping.transform.localEulerAngles += new Vector3(-90, 0, 0);

                yield return new WaitForSeconds(1 / frequency);

                var children = new List<GameObject>();
                foreach (Transform child in transform) children.Add(child.gameObject);
                children.ForEach(child => Destroy(child));

                Destroy(Instantiate(Ping.gameObject, transform.position, transform.rotation), 5f);


            }
        }

        enter = false;
        DestroyImmediate(Ping);
        StopCoroutine(SpawnRadarPing());

    }

    private void OnTriggerExit(Collider other)
    {
        bool enter = false;

        DestroyImmediate(Ping);
        Debug.Log("Exit works!");
       
        Destroy(Instantiate(Ping.gameObject, transform.position, transform.rotation), 5f);
        Ping.transform.localEulerAngles += new Vector3(-90, 0, 0);
    }

    // Use this for initialization 
    void Start()
    {
       

    }

    // Update is called once per frame 
    void Update()
    {
       

    }

}

Please use code tags: Using code tags properly

I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

Thanks Kurt-Dekker! I will try that and use code tags.

Update…
I got it to work OnTriggerExit.
I used Destroy(Instantiate(Ping.gameObject, transform.position, transform.rotation), 5f);

Now I just need to figure out how to transform them to a new rotation value.
Ping.transform.localEulerAngles += new Vector3(-90, 0, 0); isn’t working?