- Home /
GameObject not null before Instantiate
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?????
Sorry 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;
On$$anonymous$$yEvent()
{
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. $$anonymous$$y final target is create myGameObject just when On$$anonymous$$yEvent called to save memory.
Thank you
Answer by dmg0600 · Sep 20, 2014 at 10:45 AM
When 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.
How can I get my goal: just create myGameObject when On$$anonymous$$yEvent called to save memory?
Thank you
In the script you have a variable called myGameObject and you are trying to instantiate it. This variable is not the destination of the new instance, is the source from where the instance is created.
If you want it to be the destination of the new instance, make it like this.
public GameObject prefabToInstantiate;
public GameObject myGameObject;
On$$anonymous$$yEvent()
{
myGameObject = Instantiate(myGameObject) as GameObject;
}
You will have to select to object which has this script in the scene or in the hierarchy. Its properties will appear on the Inspector window. Then you have to drag the prefab you want to instantiate from the Project window to the variable prefabToInstantiate in the Inspector window.
With this done you will have a variable prefabToInstantiate which will have the prefab (therefore it won't be null) and a variable myGameObject that will be null until your event triggers.
Thank 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
If you want to Instantiate a prefab there is no other way than having a reference to it, as if you do execute Instatiate of a null variable it will instantiate nothing and give you an ArgumentException.
Of course, to have a reference, the varaible in which you have the reference cannot be null. This is a must.
The only way to do it differently is to have the prefab in Resources and load it at runtime but I think it is even more memory consu$$anonymous$$g.
public GameObject myGameObject;
On$$anonymous$$yEvent()
{
myGameObject = Instantiate(Resources.Load<GameObject>("YourPrefabName")) as GameObject;
}
To do it like this, your prefab must be in a folder named Resources in Assets.
As your answer, there's no way to create new game object when I need then destroy it when I don't need????
Your answer
Follow this Question
Related Questions
How to order newly created Objects? 1 Answer
[C#] An error occurs when trying to set a child to my GameObject in script. Why? 0 Answers
Animation plays (is visible) in scene but not in game 0 Answers
Prefab doesn't work when placed in another scene. 0 Answers
Changing value of prefab without effecting all instances. 1 Answer