Can someone help me understand what is happening when I get a null reference error, or is that too generic of a question to answer? To be more specific, its this error and I get it all the time and I dont understand what it means:
NullReferenceException: Object reference not set to an instance of an object
Can someone break this down like they are talking to a child and help me understand what is happening here?
Also, for bonus extra credit, here is the situation I am having where I am receiving this error:
When the “Fire weapon” button is pressed, I instantiate an arrow from my position.
void Update ()
{
if(Input.GetButtonDown("Fire Weapon"))
{
//Position to launch the arrow from:
launchPosition = cameraHeadTransform.TransformPoint(0,0, 0.0f);
//create the arrow
Instantiate(arrow, launchPosition, Quaternion.Euler(cameraHeadTransform.eulerAngles.x,
myTransform.eulerAngles.y, 0));
Next, inside of my if statement there is another if statement that basically says, if I continue to hold down the “Fire Weapon” button, then increasingArrowPower becomes true which is then used in a Do While loop which adds +25 to the transferArrowSpeed variable. The idea is that as long as I hold down “fire weapon” I am adding +25 to transferArrowSpeed up to a max of 200(maxArrowPower).
if(Input.GetButton("Fire Weapon"))
{
increasingArrowPower = true;
}
}
do
{
transferArrowSpeed += 25;
}
while(increasingArrowPower = true && transferArrowSpeed <= maxArrowPower);
}
THEN, if I havent put you to sleep yet, I use GetComponent to access transferArrowSpeed and use it on my arrow script (which is on the arrow) and set the amount of force to apply to the arrow.
//Get arrow speed from ShootArrow...
ShootArrow otherScript;
otherScript = GetComponent<ShootArrow>();
otherScript.transferArrowSpeed = arrowSpeed;
Then…
void FixedUpdate () {
//send the arrow forward
rigidbody.AddRelativeForce(Vector3.forward * arrowSpeed);
So apparently somewhere along the way I am getting this null reference error which by the way is on the line: otherScript.transferArrowSpeed = arrowSpeed;
You get BIG TIME bonus credit if you figure this one out lol. Thanks!