Very simple: I just made a prefab (“SpritePlane”) that’s stored in a folder (Assets/_Prefabs). The prefab just holds a single, imported plane primitive mesh.
In script, I’ve created a gameobject called Plane, and I want to instantiate the prefab, with Plane being the reference in script. So far I’ve got:
GameObject Plane;
Plane = Instantiate(SpritePlane) as GameObject;
which isn’t working, and returns a Null reference. What, oh Unity Scripting Forum, am I doing wrong here?
Thanks in advance!
You are using Instantiate incorrectly. Here is the link for the scripting reference: http://unity3d.com/support/documentation/ScriptReference/Object.Instantiate.html
Instantiate (original : Object, position : Vector3, rotation : Quaternion) : Object
Usually, you would do this:
// Assuming you're using C#
public GameObject prefab;
void Update () {
if (something happens) {
GameObject plane = Instantiate(prefab, transform.position, transform.rotation) as GameObject;
}
}
If you are in a position where you cannot dynamically link the prefab via the inspector, you could move the plane prefab to a “Resources” folder and use:
GameObject plane = Instantiate(Resources.Load("PrefabNameAsString"), transform.position, transform.rotation) as GameObject;
http://unity3d.com/support/documentation/ScriptReference/Resources.Load.html
Replacing transform.position and transform.rotation with your desired position and rotation if need be.
deleted because you just responded 