Hey guys,
I’ve got a little problem here. What my code does is to detect when you click onto an object, a script stores this object in a GameObject variable and destroys the object. I need to instantiate this object later, but the problem is that (of course) the variable just stored a reference to the object, and not the full object and the following message pops:
The question is: Is there a way to do something like var object = “GameObject” to store the full value of it and not just a reference, and use it to instantiate the object?
PS: I cannot use prefabs for this.
Thanks in advance.
You could just save the name and then use:
Instantiate(GameObject.Find(objName), position, rotation);
Answer: Don’t destroy the object. Move it to some off-screen location or deactivate it and then just move it to where you need it when you’d ordinarily be instantiating it.
Hmm, yes this stuff is tricky.
First off, I’d look at using an Asset Pool so that rather than destroying an object you hang onto it. Deactivate it, etc, but keep it (or a copy of it) around. It may simplify your life. There are some nice examples of Asset Pool implementations floating around.
If you have to destroy the object you can use Type.
Type destroyedType = typeof(aboutToBeDestroyed);
and then Resources.FindObjectsOfTypeAll to recreate it. Not sure of the exact details to do that.
I hope someone else has a better idea, but if not, please let us know what you come up with. It’s an interesting problem.
Hey thanks for answering so fast!
I just set the object to active = false. The fact is that for example, if I set to active = false and the object had some force applied to it (like gravity), when I do:
object.active = true;
transform.position = some_position;
It still has the velocity that had from the moment i set it to active = false. Curious thing, but I like it!
Once again, thank you very much!