Can't copy Gameobject into array, only refer to an existing one.

Hello, I’m currently trying to make an inventory system where I click on a gameobject, and it’s added to inventory and then the clicked object is destroyed. I’m using a GameObject array to store the clicked GameObject, but when the object in the world is destroyed the array spot changes from the GameObject to Missing. I guess this is because the array simply points to the GameObject instead of storing its own copy, how can I instead make a copy for the array, while deleting the original? I’ve looked around and found no answers or problems similar to this.

Edit: To clarify, the question is solely: How do I copy a GameObject into a GameObject instead of just making a reference?

Regards, MrGobblez.

It would be helpful (in the future) if you post your code…

In this case, it is generally more efficient simply to MOVE your object, than to copy and delete it… Just change its position, this is simpler AND more efficient


first off, if you are having trouble understanding unity, and you are having trouble accomplishing what you want in your script, how is it irrelevant to post said script?

also, take a moment to consider, why do you think scene changes would increase the number of objects in a scene? I am suggesting a method wherein you are NEVER creating/destroying objects… You are presuming that objects which have been COLLECTED stay intact, but reloading the scene creates a duplicate… There is no reason why this need be.

but I digress…

I can only guess at the details of your project, but I will assume you have a specific set of objects to collect in the scene. Let me further assume that each object has its own unique name. Taking those two basic assumptions, one way to handle this (just as an example) would be to use an array of strings, and store the name of your object (since presumably you no longer need the actual object using up memory… Then, whenever you load a different scene, it would include something like:

  function Start(){
 for(listObj in myArrayOfObjects){
 var theObject = GameObject.Find(listObj);

this will cycle through your array, find the object with the same name if it is there, and destroy it.

when you click the object to collect it, you could say something like:

 var tempArray = myArrayOfObjects;

 myArrayOfObjects = new String[ myArrayOfObjects.length + 1];
 for(var i = 0;i < tempArray.length;i++){
 myArrayOfObjects _= tempArray*;*_

myArrayOfObjects[tempArray.length] =;

this is just an example, hard to say for sure if this is exactly what you want…

to “copy” a GameObject into an array is a deceptive concept, as you have already noted, the items within the array are merely POINTERS to a GameObject. As I already mentioned, if you for some reason prefer to keep an actual copy of the GameObject in the scene, there is ABSOLUTELY NO REASON not to just use the SAME OBJECT. You are proposing DESTROYING IT AND REPLACING IT WITH AN EXACT REPLICA, pointless (unless you’re Steven Wright :>) If you copy the object, what does it accomplish?
For the sake of levity, here’s how:
myArray[nextSlot] = Instantiate(myClickedObject,somePosition,Quaternion.rotation);
don’t do it that way, it’s bad practice, instead why not just
myArray[nextSlot] = myClickedObject;
myClickedObject.transform.position = somePosition;
this accomplishes EXACTLY the same result, without the need for a costly Instantiate