how do I Instantiate gameobject and add to array and then move the previous instantiated gameobject above the current?

I created a UI text prefab and instantiated it into an array. I want to move the previous instantiated object above the current one. I’m still learning programming and this one is holding me off. Any help will be greatly appreciated. See below for the details.

public class DisplayLogs : MonoBehaviour {

    public GameObject textLogs;

    GameObject[] logtext;

    int arrayPosition = 0;

    void Awake()
    {
        logtext = new GameObject[6];
        arrayPosition = 0;
    }

    public void addLog(string text)
    { 

        textLogs.GetComponent<Text>().text = text;
        print("ArrayPosition: " + arrayPosition);
        logtext[arrayPosition] = Instantiate(textLogs).gameObject;
        RectTransform tempRect = logtext[arrayPosition].GetComponent<RectTransform>();
        logtext[arrayPosition].transform.SetParent(this.transform);
        tempRect.transform.localPosition = textLogs.transform.localPosition;
        tempRect.transform.localPosition = new Vector3(textLogs.transform.localPosition.x, textLogs.transform.localPosition.y + (arrayPosition * 15), textLogs.transform.localPosition.z);
        tempRect.transform.localScale = textLogs.transform.localScale;
        tempRect.transform.localRotation = textLogs.transform.localRotation;
        Destroy(logtext[arrayPosition].gameObject, 5f);
      
        arrayPosition = arrayPosition + 1;

        if (arrayPosition == 6)
        {
            arrayPosition = 0;
        }

    }

}

You could do it in 2 ways:

  1. You could reload your array every time you add a new index:

void ArrayFunction{
public GameObject arr;
//Default the size to a size of 1
arr = new GameObject[1];

//Now your code to update the array, however you are doing it, heres one method
InvokeRepeating("AddOnetoArray",3f,3f); //Adds an item to the array every 3 seconds.

}

void AddOnetoArray(){
//clone array before changing it, store it in a temp variable
GameObject[] tempArr;
tempArr = arr;

//Update the array
arr = new GameObject[arr.length]; //Since we set it to one before, it will read index 0 as 1, so this will be 2.

//Instantiate a new gameobject, and give it a variable name
GameObject bob = (GameObject) Instantiate(yourPrefab,yourPrefabPos,yourPrefabRot); //Spawn your actual object

//Load your actual object into your array
arr[0] = bob;

//The old array has just been overloaded, but we still have tempArr with the old data, so now lets load it back in
for(int i = 1; i < tempArr.length - 1; i++){ //Start at 1, so we never overright the 0th index, being the most recent addition
arr *= tempArr[i - 1]; //i - 1 so that it starts at 0, since we start at 1.*

}
}
1. You could also sort it automatically:
void ArrayFunction{
public GameObject[] arr;
public bool sortArray;
//Default the size to a size of 1
arr = new GameObject[1];

//Now your code to update the array, however you are doing it, heres one method
InvokeRepeating(“AddOnetoArray”,3f,3f); //Adds an item to the array every 3 seconds.

}

void AddOnetoArray(){
//clone array before changing it, store it in a temp variable
GameObject[] tempArr;
tempArr = arr;

//Update the array
arr = new GameObject[arr.length]; //Since we set it to one before, it will read index 0 as 1, so this will be 2.

//Instantiate a new gameobject, and give it a variable name
GameObject bob = (GameObject) Instantiate(yourPrefab,yourPrefabPos,yourPrefabRot); //Spawn your actual object

//Give bob a specific name, adding its index location as an identifier
bob.name = "Cubical " + arr.length - 1;

//Load your actual object into your array
arr[0] = bob;

//Run a function that sorts out the array for us. You could also move this functions code into Update instead, which may be more accurate (quicker) but can eventually get laggy when you have something like a 200+ array.
sortArray = true;
SortThisOut();
}
}

public void SortThisOut(){
if(sortArray == true){
//Copy the array for a second in a temp array, case we mess up
GameObject[] tempArr = arr;
for(int i = 0; i < arr.length; i++){
if(tempArr[tempArr.length - i].name.Last(i)){ //If the last gameobject in the array ends with the current number we are on…
arr = tempArr[tempArr.length - i];
}
}
sortArray = false;
}
}
The second method is a much more simplified version of “bubble sorting”, which using bubble sorting is I personally find annoying… But its very efficient, so that is also an option.
All this code is untested by the way… So if you run into any problems, I can help you sort (no pun intended) through it.