- Home /
how to set Object reference to an instance of an object
I get this error:
NullReferenceException: Object reference not set to an instance of an object Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Type[] cacheKeyTypes, System.Object[] args, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.Dispatch (System.Object target, System.String cacheKeyName, System.Object[] args, Boo.Lang.Runtime.DynamicDispatching.DispatcherFactory factory) Boo.Lang.Runtime.RuntimeServices.Invoke (System.Object target, System.String name, System.Object[] args)
Here's my code
public var myspeed = 3.0;
public var cratePrefab:Transform;
function Update () {
//find out if fire button pressed if(Input.GetButtonDown("Fire1")) { //Create the prefab var crate = Instantiate (cratePrefab, transform.position, Quaternion.identity);
//add force to the prefab
crate.Rigidbody.AddForce(transform.forward * 2000);
}
}
What is wrong?
Answer by Mike 3 · Jan 31, 2011 at 04:19 AM
Oh god - evil evil js not giving you an error about this:
crate.Rigidbody.AddForce(transform.forward * 2000);
should be
crate.rigidbody.AddForce(transform.forward * 2000);
It may also be worth doing
var crate : Transform = Instantiate (cratePrefab, transform.position, Quaternion.identity);
so that it stops allowing dynamic function calling on the crate reference
Edit:
I would also use AddRelativeForce so that you can use Vector3.forward instead of transform.forward, should be a little less computationally expensive