Instantiating Prefabs

I’m missing something very basic, but just can’t seem to get this to work.
I have a Prefab “Spacecraft” defined in the Project tab. I’d like to instantiate it via code so that it is placed in a variable"

var spacecraft : GameObject;

...

spacecraft = Instantiate (GameObject.Find("Spacecraft"), GameObject.Find("Pad").transform.position, Quaternion.identity);

which results in NullReferenceException, no doubt because “Spacecraft” is still a Prefab.
I have also tried:

spacecraft = Instantiate (Spacecraft, GameObject.Find("Pad").transform.position, Quaternion.identity);

which results in Unknown identifier: ‘Spacecraft’.
and

spacecraft = Instantiate ("Spacecraft", GameObject.Find("Pad").transform.position, Quaternion.identity);

which barfs because Instantiate does not take a string. (yes I am grasping at straws!) :slight_smile:

FYI the Spacecraft Prefab is in a folder marked “Subassemblies”
Any clues would be appreciated!
Thanks
Joe

You can’t use the same variable name to store the new instantiated prefab, you need to make a new one.

var instantiatedSpacecraft = Instantiate (Spacecraft, GameObject.Find(“Pad”).transform.position, Quaternion.identity);

Thanks, I thought that spacecraft and Spacecraft were different. But no matter, this still doesn’t work:

spacecraft = Instantiate (SpacecraftPrefab, GameObject.Find("Pad").transform.position, Quaternion.identity);

(I renamed the prefab in the project tab because I was using the variable in a lot of places)

Still get a compile error: Unknown identifier: ‘SpacecraftPrefab’.

Did you forget to change the name of SpacecraftPrefab up top, in your exposed var?

It needs to be like this:

var  spacecraftPrefab : GameObject;

...

var spacecraft = Instantiate (spacecraftPrefab, GameObject.Find("Pad").transform.position, Quaternion.identity);

Sorry for being so thick… I am trying to understand how spacecraftPrefab gets associated with my prefab in the project pane. I’m getting an error with this as well.

Thanks

Have you dragged your prefab to the “spacecraftPrefab” var ?

Select the object where you have this script,
then look at inspector panel under your script, something like:

spacecraftPrefab None (GameObject) < Drag&drop your prefab object here

Just to be clear: I have not dragged prefab into the Hierarchy pane. I was assuming that instantiation would accomplish that. Basically, I’ll have couple of folders in the Project pane filled with prefabs and need to assemble them in the hierarchy using JS. Here is the structure in the Project pane:

Subassemblies
SpacecraftPrefab
StagePrefab
Systems
Engine Prefab

etc…

mgear: If I understand correctly, script that is assembling the “spacecraft” must have variables for every possible prefab, and the prefabs must be dragged onto those variables in the Inspector. Then my script could pick and choose using those variables as the source?

I guess there are other ways, but thats the easy way (drag drop into ‘variables’ on inspector)

Try it atleast, if your inspector shows “spacecraftPrefab None (GameObject)”

Or maybe post inspector panel screenshot of your object(that has the script)

I’ll do that when I get back to my home office tonight. Thanks, mgear!!

Ohh! I had no idea that you weren’t dragging your prefab to the exposed variable.

You don’t want to drag it to the hierarchy, because that would, in essence, instantiate a prefab.

Once you declare a public var like:

public var  spacecraftPrefab : GameObject;

outside of any functions in a script, that variable will be exposed in the inspector editor when you click on the object in the scene or project that has this script.

Then you have to drag the prefab from your project (not the hierarchy) into the exposed var in the inspector.

THEN when you use this var as an argument in the instantiate function:

var spacecraft = Instantiate (spacecraftPrefab, GameObject.Find("Pad").transform.position, Quaternion.identity);

The spacecraftPrefab var will actually be a reference to the prefab in the project folder (otherwise it was just a null var of type GameObject).

This makes so much more sense. Sorry for the missing info and thanks to both legend411 and mgear. Can’t wait to try this!
Joe