hi i keep getting this error, however the game runs fine…
here’s the line of script that gives the error:
bullit.rigidbody.AddForce( transform.forward * 2000);
2 Answers
2It is quite certain that one of the elements is null in that sentence, so:
bullit is null
or it has no attached rigidbody
you should try
if (bullit != null)
if(bullit.rigidbody != null)
bullit.rigidbody.AddForce( transform.forward * 2000);
else
Debug.LogWarning("No bullit rigidbody");
else
Debug.LogWarning("bullit is null and it shouldn't be");
well that means that 'transform.forward' is null, for strange that this might seem. what have you attached this script to? Is 'bullit' a prefab? Since it's probably a prefab, you MUST instantiate it into the game world to be able to manipulate it. Try changing 'transform.forward' with 'Vector3.forward', just to be sure.
– roamcelyou are not following, I believe: bullit must be an instance of an object. If you're not using 'instantiate' on it, it's clearly referring to something not suitable, for example a prefab.
– roamcelHere’s a working copy of how to apply force to an object. Relative will allow you to apply it relative to it’s heading. AddForce applys force relative to world space, NOT your objects heading.
var shipThrust : float; // Quantatative force you want applied.
var rotationForce : float; // applies a rotation force to the mesh (torque)
rigidbody.AddRelativeForce(Vector3.forward * Time.deltaTime * shipThrust); // Applies Z-Axis Forces to the whole Mesh
rigidbody.AddRelativeTorque(Vector3.up * Time.deltaTime * rotationForce); // Applies Y-Axis Rotation (think left turn/right turn)
These forces get applied to the transform this script is attached to. no need to call this transform seperately.
Good old TornadoTwins huh?
– FLASHDENMARK