Issue accessing a GameObject through an Arraylist

Hi
I create dynamically a number of GameObject (spheres), and add them to an arraylist at the same time.

private ArrayList wake1 = new ArrayList();

void Update () {
GameObject sphere_1 = GameObject.CreatePrimitive(PrimitiveType.Sphere);
wake1.Add(sphere_1);

DestroyEndWake();
}

void DestroyEndWake() {
if (this.wake1.Count > 20) {
	GameObject w = wake1[0];
	Destroy(w);
	wake1.RemoveAt(0);
}

My goal is to access later on the GameObject to destroy it (I especially want to destroy the older ones).
I get in trouble because when I access an item from this arraylist, it seems like it is not recognized as a GameObject, then I don’t have access to the GameObject method, such as Destroy.

The compiler error for the code above is:
error CS0266: Cannot implicitly convert type object' to UnityEngine.GameObject’. An explicit conversion exists (are you missing a cast?)

Thanks for your help

Because the ArrayList does not know what type its members are, you cannot assume that its values are GameObjects. Instead, you must cast it:

GameObject w = wake1[0] as GameObject;

Alternatively, consider using a List with generics instead of an ArrayList:

private List<GameObject> wake1 = new List<GameObject>();

If you do this you will not have to cast the value when accessing it. If you do use List, be sure to import that package by placing

using System.Collections.Generic;

at the top of your script.

Great! Thanks for your answer.