Move/Shift Objects in array up then move the first element to the last index [closed]

This is called Object Pooling. You do not need to remove array from first or last to do this. There are many ways to archive this in Unity.

Method 1 (Recommended):

Even though that works, it unnecessary and inefficient to move the Objects in the array. You can have an index that moves around. It starts from 0 and each time you request an Object, you increment it by one. If the index equals to Array.Length - 1, reset index back to 0.

public class ArrayObjectPooling
{
    int amount = 10;
    GameObject[] objArray;
    int currentIndex = 0;

    public ArrayObjectPooling(GameObject objPrefab, string name, int count)
    {
        amount = count;
        objArray = new GameObject[amount];

        for (int i = 0; i < objArray.Length; i++)
        {
            objArray[i] = UnityEngine.Object.Instantiate(objPrefab, Vector3.zero, Quaternion.identity);
            objArray[i].SetActive(false);
            objArray[i].name = name + " #" + i;
        }
    }

    //Returns available GameObject
    public GameObject getAvailabeObject()
    {
        //Get the first GameObject
        GameObject firstObject = objArray[currentIndex];

        //Move the pointer down by 1
        shiftDown();

        return firstObject;
    }

    //Returns How much GameObject in the Array
    public int getAmount()
    {
        return amount;
    }

    //Moves the current currentIndex GameObject Down by 1
    private void shiftDown()
    {
        if (currentIndex < objArray.Length - 1)
        {
            currentIndex++;
        }
        else
        {
            //Reached the end. Reset to 0
            currentIndex = 0;
        }
    }
}

USAGE:

public GameObject prefab;

void Start()
{
    ArrayObjectPooling arrayPool = new ArrayObjectPooling(prefab, "Springs", 10);
    GameObject myObj = arrayPool.getAvailabeObject();
}

Method 2:

List is enough to do this if the pool is small. Please take a look at Unity’s official tutorial for Object Pooling implemented this. It is unnecessary to post the code here.

You simply disable the GameObject in the List to recycle it. Next time you need a new one, loop over the List and return the first disabled GameObject in the List. If it is enabled, that means that it is still in use.

If no disabled GameObject is found in the List, then you can Instantiate a new one, add it to the list and then return it. This is the easiest way of doing this. Just watch the Unity tutorial for more information.


Method 3:

Use Queue or Stack. You can queue and enqueue or push/pop the Objects from the pool. There are many implementation out there if you do a simple reseach-search on this.


Method 4:

This is only mentioned because this was your original question about how to shift objects up in an array and then put the first value in the last index of the array. You should not not use this. It is only here just to show that it can be done.

public class ArrayObjectPooling
{
    int amount = 10;
    public GameObject[] objArray;

    public ArrayObjectPooling(GameObject objPrefab, string name, int count)
    {
        amount = count;
        objArray = new GameObject[amount];

        for (int i = 0; i < objArray.Length; i++)
        {
            objArray[i] = UnityEngine.Object.Instantiate(objPrefab, Vector3.zero, Quaternion.identity);
            objArray[i].SetActive(false);
            objArray[i].name = name + " #" + i;
        }
    }

    //Returns available GameObject
    public GameObject getAvailabeObject()
    {
        //Get the first GameObject
        GameObject firstObject = objArray[0];

        //Move everything Up by one
        shiftUp();

        return firstObject;
    }

    //Returns How much GameObject in the Array
    public int getAmount()
    {
        return amount;
    }

    //Moves the GameObject Up by 1 and moves the first one to the last one
    private void shiftUp()
    {
        //Get first GameObject
        GameObject firstObject = objArray[0];

        //Shift the GameObjects Up by 1
        Array.Copy(objArray, 1, objArray, 0, objArray.Length - 1);

        //(First one is left out)Now Put first GameObject to the Last one
        objArray[objArray.Length - 1] = firstObject;
    }
}

USAGE:

public GameObject prefab;

void Start()
{
    ArrayObjectPooling arrayPool = new ArrayObjectPooling(prefab, "Springs", 3);
    GameObject myObj = arrayPool.getAvailabeObject();
}

Leave a Comment