Hi all,
I have a problem with Initiate function:
I declare a GameObject as a public variable in my C# script:
public GameObject myGameObject;
Then in editor I drag my prefab into my C# script position.
I debug I recognize that before I call Instantiate(myGameObject), myGameObject has existed ( not null), Why???
1 Answer
1When you create the variable GameOBject myGameObject and pass it a prefab, you are giving it a value, therefore it is not null.
What Instantiate does is to create a copy of the object you pass as a parameter, not to store a GameObject into that variable.
As an example:
public GameObject myGameObject;
void Start()
{
Instantiate(myGameObject);
}
This will create a copy of whatever GameObject (from the scene or a prefab) you have dragged to myGameObject variable, but you will have no reference to it. If you want to keep a reference for the new copy you can do it like this:
public GameObject myGameObject;
private GameObject copyOfGameObject;
void Start()
{
copyOfGameObject = Instantiate(myGameObject) as GameObject;
}
Thank you so much I have 2 questions here: 1. > When you create the variable > GameOBject myGameObject and pass it a > prefab, you are giving it a value, > therefore it is not null. ==> So where myGameObject: I cannot see it in my hierachy or in editor view. 1. How can I get my goal: just create myGameObject when OnMyEvent called to save memory? Thank you
– lamxung55Thank you but your way is not match to my goal. I want to save memory loaded, by your way I still waste prefabToInstantiate which is not null before event triggers. Any other idea
– lamxung55As your answer, there's no way to create new game object when I need then destroy it when I don't need????
– lamxung55Thank you so much dmg, > Anyway, instantiating and destroying > GameObjects is very very inefficient ==> This option is very valueable for me. I will think deeply about it One more little question, please. When I drag my prefab into the editor, you said that it was created( not null), yes that's correct but I wonder where that game object is, I cannot see it in game view also in hierachy window?
– lamxung55When you drag the prefab to the variable in the editor, it saves a reference to the prefab, hence it is not created automatically in the scene but just kept in the project until you instantiate it. That's why you can't see it in hierarchy.
– dmg0600
I don't know what you are saying here. If you are getting a null reference despite the fact you dragged and dropped to initialize 'myGameObject', look for a second copy of the script on another game object.
– robertbuSorry for something not clear in my question. I re-write my question: I have a prefab(called myGameObject). I want to create my prefab just when my custome event called: public GameObject myGameObject = null; OnMyEvent() { Instantiate(myGameObject); } I think myGameObject will be null before I call Instanitate(myGameObject) but in fact it is not null. Do I miss something here, please tell me. My final target is create myGameObject just when OnMyEvent called to save memory. Thank you
– lamxung55