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.