Instantiate prefab and save in variable

So, i instantiate a gameobject, but how do i save this created gameobject in another variable.

I have a GameObject variable where i store my prefab in, then i instantiate the GameObject but how do i save this?

I did this, but this gave me an error:

GameObject createdObject = GameObject.Instantiate(Foundation, Cube1InstantiatePos, CameraRotation);

Anyone has an idea how to do this?
Thanks in advance!

GameObject.Instantiate returns an Object.

You’ll need to cast this to a GameObject first.

So:

GameObject createdObject = GameObject.Instantiate(Foundation, Cube1InstantiatePos, CameraRotation) as GameObject;

Should work

Next time, please also include whatever errors you get, since it will make it much easier to debug your code.

This one isn’t too bad though. Instantiate() returns an Object (which is the same type as Foundation). So if Foundation is a GameObject, Instantiate() will then return an Object which can be casted to a GameObject like such:

GameObject createdObject = GameObject.Instantiate(Foundation, Cube1InstantiatePos, CameraRotation) as GameObject;

This works because GameObject is derived from Object.