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);
}