- Home /
NullReferenceException: Object reference not set to an instance of an object
I get the NullReferenceException: Object reference not set to an instance of an object error at the moment and can not seem to find out why.
It says the problem is with this code:
if (bullet)
{
Instantiate(bullet,bulletSpawn.transform.position, bulletSpawn.tranform.rotation);
waitTillNextFire = 1;
}
Here is how I declared them:
var waitTillNextFire : float = 0;
var bullet : GameObject;
var bulletSpawn : GameObject;
And I have also put in the objects in the inspector as shown here:
Here's the whole script :)
var CameraObject : GameObject;
@HideInInspector
var targetXRotation : float;
@HideInInspector
var targetYRotation : float;
@HideInInspector
var targetXRotationV : float;
@HideInInspector
var targetYRotationV : float;
var rotateSpeed : float = 0.3;
var holdHeight : float = -0.5;
var holdSide : float = 0.5;
var ratioHipHold : float = 1;
var hipToAimSpeed : float = 0.1;
@HideInInspector
var ratioHipHoldV : float = 1;
var fireSpeed : float = 15;
@HideInInspector
var waitTillNextFire : float = 0;
var bulletSpawn : GameObject;
var bullet : GameObject;
function Update ()
{
if (Input.GetButton("Fire1"))
{
if (waitTillNextFire <= 0)
{
if (bullet)
{
Instantiate(bullet,bulletSpawn.transform.position, bulletSpawn.tranform.rotation);
waitTillNextFire = 1;
}
}
}
waitTillNextFire -= Time.deltaTime * fireSpeed;
if (Input.GetButton("Fire2"))
ratioHipHold = Mathf.SmoothDamp(ratioHipHold, 0, ratioHipHoldV, hipToAimSpeed);
if (Input.GetButton("Fire2") == false)
ratioHipHold = Mathf.SmoothDamp(ratioHipHold, 1, ratioHipHoldV, hipToAimSpeed);
transform.position = CameraObject.transform.position + (Quaternion.Euler(0,targetYRotation,0) * Vector3(holdSide * ratioHipHold, holdHeight * ratioHipHold, 1));
targetXRotation = Mathf.SmoothDamp( targetXRotation, CameraObject.GetComponent(MouseLookScript).xRotation, targetXRotationV, rotateSpeed);
targetYRotation = Mathf.SmoothDamp( targetYRotation, CameraObject.GetComponent(MouseLookScript).yRotation, targetYRotationV, rotateSpeed);
transform.rotation = Quaternion.Euler(targetXRotation, targetYRotation, 0);
}
Answer by Kryptos · Sep 07, 2012 at 11:55 AM
Does your screenshot represents the state of the inspector at the moment this error occured (during play mode) or just the state when editing?
It is possible that one of the two references become null during the game. Especially if you destroy one of them after the first shoot. You should keep a reference to the instantiated prefab so that you can destroy the instance when you don't need it anymore:
private var usedBullet : GameObject;
// ...
usedBullet = Instantiate(...);
// somewhere later...
Destroy(usedBullet);
Well that was taken during editing but its the same in play mode. I haven't put any code to destroy the bullet, it doesn't even move right now it just creates it at the bullet spawn... or it should.
Oh and the error only occurs when I click to fire.
Answer by Cripple · Sep 07, 2012 at 12:32 PM
Can you share the entire script code please ? It may be related to the Fire call.