- Home /
Calling Instantiate from a function within a class
Trying to call Instantiate from within a function in a class. Here's what it looks like:
class Marker {
function Action() {
Object.Instantiate(Resources.Load("t_obj"), Vector3(10.0, 20.0, 100.0), Component.Transform.rotation);
}
}
The error I get is: "NullReferenceException: Object reference not set to an instance of an object."
If I call Instantiate outside of the class (as part of an OnMouseDown function in another file) it works fine.
Fixed my problem. The correct Instantiate line should read:
UnityEngine.Object.Instantiate(Resources.Load("t_obj"), Vector3(10.0, 20.0, 100.0), Quaternion.identity);
Working as it should now.
Answer by sneftel · Aug 10, 2011 at 09:56 PM
Component.Transform.rotation
isn't a real thing. Perhaps you mean transform.rotation
?
Without 'Component' I get the error: "$$anonymous$$ identifier: 'transform'."
Answer by AaronG · Aug 11, 2011 at 10:59 AM
You don't need to use 'Object' as a prefix. IE...
Instantiate (prefab, transform.position, transform.rotation);
http://unity3d.com/support/documentation/ScriptReference/Object.Instantiate.html