Retrieve GameObject from array

I haven’t been able to find an answer to my question for my specific case so here I am. I want to have an array of game objects and be able to take one out based on the number that it appears as in the array. If anybody has any answers, that would be greatly appreciated. If not I have a backup plan.

GameObject[] gObjects = new GameObject[10];
gObjects.Add(objectA);
GameObject newObjectA = gObjects[0];

I think that’s what you mean.

Resizing an array is not a time efficient task.

Could you implement your code as a List ?

Use a collection such as a List instead.

Or if you mean you just want to select an object from an array, use an index value.
Example:

object[] myArray = new object[10];

object myObject1 = myArray[0]; //Selects the first object in the array
object myObject1 = myArray[4]; //Selects the third object in the array
object myObject3 = myArray[myArray.Length]; //Selects the last object in the array

This works in all collections as well.

myArray[myArray.Length] will give you an out of range exception, though.

Yup it will. It should be Length - 1

For example an array with one element will have a size of 1, but the index of said element would be 0.
Cheers.

Right, my mistake. It should be:

myArray[myArray.Length - 1];