The behavior of the objects after spawning are inconstant where maybe about 50% of the time objects do return to the pool but the other 10% don’t. They just hangout offscreen in limbo.
This is the line that actually commands the objects to return to the pool:
NewObjectPool.instance.PoolObject(SpawnedObject);
Heres the code I have for my destroy script:
using UnityEngine;
using System.Collections;
public class DestoryOverTime : MonoBehaviour
{
public Collider2D SpawnedObjectCollider;
public GameObject SpawnedObject;
private Animator SpawnedObjectAnimator;
private Camera PlayerCam;
private Plane[] planes;
public static float ReturnTime = 15;
public bool poolAfterComplete = true;
public bool IsVisible;
void OnEnable()
{
//Debug.Log ("script was enabled");
PlayerCam = Camera.main;
planes = GeometryUtility.CalculateFrustumPlanes (PlayerCam);
SpawnedObjectCollider = SpawnedObject.GetComponent<Collider2D> ();
}
void Update ()
{
if (GeometryUtility.TestPlanesAABB(planes, SpawnedObjectCollider.bounds))
{
IsVisible = true;
//Debug.Log(SpawnedObject.name + ": Has been Detected!");
}
else
{
IsVisible = false;
//Debug.Log("Nothing has been detected");
}
if (IsVisible)
{
Invoke ("StartTheProcess", 2f);
//Debug.Log("Start process is invoked");
}
}
public void StartTheProcess()
{
Invoke ("StartProcess", 2f);
//Debug.Log("Start process is about to start");
}
public void StartProcess ()
{
if (SpawnedObject)
{
//StartCoroutine (Coro());
ReturnSpawnedObject();
}
else
{
Invoke ("StartTheProcess", 2f);
//Debug.Log ("spawnedobject is false, and is about to return to method");
}
}
public void ReturnSpawnedObject()
{
//OnBecameInvisible() old, the void used to be this.
if (!IsVisible)
{
Debug.Log ("is render = " + IsVisible);
Invoke ("ResetEffect", ReturnTime);
Debug.Log ("spawned object is about to return");
}
else
{
Invoke ("StartProcess", 2f);
Debug.Log("Spawned object is returning and is visible");
}
}
public virtual void ResetEffect ()
{
if(poolAfterComplete)
{
if (!IsVisible)
{
NewObjectPool.instance.PoolObject(SpawnedObject);
Debug.Log("Fish is returning back to pool");
}
else
{
Invoke("LastResortReturn", 15f);
//Debug.Log("object is visible and can't return to pool yet");
}
}
else
{
Invoke("LastResortReturn", 15f);
//Debug.Log("Object wasn't pooled and is returning");
}
}
public void LastResortReturn ()
{
NewObjectPool.instance.PoolObject(SpawnedObject);
Debug.Log ("last Resort has been reached return to pool now");
}
}