C # Objects not always returning to pool.

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");
    }
}

Bump

Still need some help on this issue.

what are you trying to do? :slight_smile:

well, we just want the objects to spawn move though the screen and then when off screen return to the object pool.

ah ok i see and you want to do that with some delay before your fish returns to inscreen?
why not simply make your screen bounds bigger then your screenview and use a continues world (like pacman)?

not quite the fish are not always returning to the pool after moving off-screen, and sorry for the late reply.

it’s hard to see why with the information at hand.
can you explain your logic a bit more?

Never mind I ended up fixing it, I had to rewrite how my isvisible boolean was used. Used triggers, vs the collider bounds being detected within the camera view. Also instead of waiting a certain amount of time before the fish returned to the pool they just returned when they entered the trigger.

Sometimes the simplest solutions are the better solutions.

Well, thanks allot for your support, jister :slight_smile: !