- Home /
"The prefab you want to instantiate is null" what am I doing wrong nested...?
I have a game object that's created at runtime, I've attached a script to it after creating it, that script holds an instantiation of an object should something happen.... because this game object is created at runtime, and the script is attached after it's creation, I can't access the inspector to set the prefab...
So I'm trying to add it in code...
something like this: this script is attached to the object that's waiting for the condition to create the prefab:
var PS_top_ordered : GameObject;
PS_top_ordered = GetComponent("PS_top_Ordered");
And later, inside the update (){ is this:::
if (Input.GetKeyDown ("a")){
Instantiate(PS_top_ordered, transform.position, transform.rotation);
}
Answer by taoa · Dec 23, 2010 at 03:53 PM
The GetComponent function returns a component, not a prefab of a game object. Yet you assign it into PS_top_ordered which is a game object.
I can't even believe the Javascript compiler allows you to do that! Use C# :)
so how do I set the variable PS_top_ordered to the prefab "PS_top_Ordered" which is sitting in my Project?
If I do it like this, it tells me I'm trying to make the variable into a string>>>> var PS_top_ordered : GameObject; PS_top_ordered = ("PS_top_Ordered");
Ins$$anonymous$$d of assigning PS_top_ordered in your script, since your script should be attached to a game object, slide (click->drag) your PREFAB named PS_top_ordered into the property in the inspector, then you can instantiate at will! :)
I can't do that. the Script is nested. Sits on a game object that's created at runtime, from a prefab. The script is added to the instantiation of the prefab at game start using "addcomponent" which works, (pulling the script from the inspector to attach to the newly created "parent" prefab) but getting this second layer deeper, and pulling a prefab from the inspector doesn't?
The Javascript compiler doesn't allow it, if you use #pragma strict.