Object pooling.release in coroutine

Hello I’m trying to return the object into pool after a delay on collision but i face this weird problem where on collision of one object it returns two objects to pool.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class CarController : MonoBehaviour
{
    public GameController gameController;
    public Game.Scripts.Play.MoveParent moveParent;
    public float force;
    public float disappearingTime;
    public Rigidbody rb;
    // Start is called before the first frame update
    void Start()
    {
        gameController = GetComponentInParent<GameController>();
        GameObject GO = GameObject.Find("PlayerWrapper");
        moveParent = GO.GetComponent<Game.Scripts.Play.MoveParent>();
    }
    public void OnCollisionEnter(Collision collision)
    {
        if (collision.collider.tag == "Player")
        {
            
            StartCoroutine(DelayAction(collision));
            //DelayAction(collision);
        }
    }
    
    private void OnTriggerEnter(Collider collision)
    {
        if (collision.tag == "Player")
        {
            StartCoroutine(DelayAction(collision));
        }
    }
    
    
    IEnumerator DelayAction(Collision collision)
    {
        var rb = collision.gameObject.GetComponent<Rigidbody>();
        rb.gameObject.GetComponent<ToggleRagdoll>().RagdollActive();
        rb.isKinematic = false;
        rb.AddForce(new Vector3(0, 0, -force));
        gameController.playerList.Remove(rb.gameObject.GetComponent<Model>());
        moveParent.animList.Remove(rb.gameObject.GetComponent<Animator>());
        rb.gameObject.GetComponent<NavMeshAgent>().enabled = false;
        rb.gameObject.GetComponent<NavMeshObstacle>().enabled = false;
        rb.gameObject.GetComponent<Model>().enabled = false;
        yield return new WaitForSeconds(disappearingTime);     
        gameController._pool.Release(rb.gameObject.GetComponent<Model>());
    }
    
     /*
    public void DelayAction(Collision collision)
    {
        var rb = collision.gameObject.GetComponent<Rigidbody>();
        rb.gameObject.GetComponent<ToggleRagdoll>().RagdollActive();
        rb.isKinematic = false;
        rb.AddForce(new Vector3(0, 0, -force));
        gameController.playerList.Remove(rb.gameObject.GetComponent<Model>());
        moveParent.animList.Remove(rb.gameObject.GetComponent<Animator>());
        rb.gameObject.GetComponent<NavMeshAgent>().enabled = false;
        rb.gameObject.GetComponent<NavMeshObstacle>().enabled = false;
        rb.gameObject.GetComponent<Model>().enabled = false;
        gameController._pool.Release(rb.gameObject.GetComponent<Model>());
    }
     */
 
}

I have tested without Coroutine, it works perfectly fine but objects on collision must disappear after couple seconds.

Maybe the coroutine runs twice? (more collision can happen when not destroying it immediately)
Add a check that it can only run once maybe

1 Like

There are other functions in the coroutine like removing from a list just like the pool, they work fine, they don’t work twice. I didn’t add the whole code of it here.

Then add the whole code if you want support for the issue. I just help based on what I see.
Also adding logs to see if it runs once or multiple times

2 Likes

Also, have you actually checked the coroutine only runs once? Add a Debug.Log statement at the beginning and make sure you don’t have “collapse” enabled in the console window. Maybe include Time.realtime in the text ^^. Also add a context object to the Debug.Log call so you can tell which object has produced this log message.

1 Like

I have updated the code with full version. I will also update the objects screenshots for better understanding as there is no double colliders to make it work twice or such kind of thing

1 Like

What kind of pool do you actually use? Is it your own solution? Maybe add a debug log to the release method so you can check the stack trace of each log statement to see where it comes from.