Really quick question about Stacks & Instantiate

So this has been driving me nuts for the past 4.5 hours.

I’m trying to Instantiate a prefab from a stack that has GameObjects pushed in it.

The error I’m getting is: Assets\Stack\stack_instantiate.cs(29,25): error CS1503: Argument 1: cannot convert from ‘object’ to ‘UnityEngine.Object’

Replacing stack.Pop() in Instantiate with cube_obj works great, but for some reason I’m getting this error when using stack.Pop().
Meanwhile, Debug.Log() shows exactly the same for both cube_obj & stack.Pop()'ing the cube_obj.

Only thing I found online was to use as GameObject at the end of Instantiate, but it didn’t work…
Using as GameObject just returns one more error, this one: Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

Here’s the code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class stack_instantiate : MonoBehaviour
// Object spawn object
public GameObject spawner;

// Objects database that will/can be pushed into stack
public GameObject cube_obj;

// Create stack
Stack stack = new Stack();

// Start is called before the first frame update
void Start()

// Update is called once per frame
void Update()
    if(Input.GetKeyDown("space") & stack.Count != 0)
        Instantiate(stack.Pop(), spawner.transform.position, Quaternion.identity);


You used the untyped Stack class which stores System.Object references. The Instantiate method can only clone objects derived from UnityEngine.Object. You have two options: First cast the returned System.Object reference to GameObject (or UnityEngine.Object) before you pass it to Instantiate. The second option, which is the better one, is to use the typed generic Stack<T> class with a proper type argument. Specifically

Stack<GameObject> stack = new Stack<GameObject>();

Note that in your specific code you only push one object onto the stack but you try to pop two objects. So the second call Debug.Log(stack.Pop()); would cause an error.

Thanks, you rock!
Couldn’t find that much documentation regarding Stacks because Google kept throwing Data Oriented Technology Stack in my face.

Anyway, thanks again!