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"
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.
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?
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).