I am trying to make my player shoot and I keep getting this error: Object Reference not set to an instance of the object PlayerController.Update() (at Assets/Scripts/PlayerController.cs:34).
Here are my scripts
Gun Script-
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (AudioSource))]
public class Gun : MonoBehaviour {
public enum GunType {Semi,Burst,Auto};
public GunType gunType;
public float rpm;
public Transform spawn;
private float secondsBetweenShots;
private float nextPossibleShootTime;
public Transform shellEjectionPoint;
public Rigidbody shell;
private LineRenderer tracer;
void Start() {
secondsBetweenShots = 60 / rpm;
if (GetComponent<LineRenderer> ()) {
tracer = GetComponent<LineRenderer>();
}
}
public void Shoot() {
if (CanShoot()) {
Ray ray = new Ray (spawn.position, spawn.forward);
RaycastHit hit;
float shotDistance = 20;
if (Physics.Raycast (ray, out hit, shotDistance)) {
shotDistance = hit.distance;
}
nextPossibleShootTime = Time.time + secondsBetweenShots;
audio.Play();
if (tracer) {
StartCoroutine("RenderTracer", ray.direction * shotDistance);
}
Rigidbody newShell = Instantiate(shell,shellEjectionPoint.position,Quaternion.identity) as Rigidbody;
newShell.AddForce(shellEjectionPoint.forward * Random.Range(150f,200f));
}
}
public void ShootContinuous() {
if (gunType == GunType.Auto) {
Shoot();
}
}
private bool CanShoot() {
bool canShoot = true;
if (Time.time < nextPossibleShootTime) {
canShoot = false;
}
return canShoot;
}
IEnumerator RenderTracer(Vector3 hitPoint) {
tracer.enabled = true;
tracer.SetPosition(0, spawn.position);
tracer.SetPosition(1, spawn.position + hitPoint);
yield return new WaitForSeconds(.015f);
tracer.enabled = false;
}
}
PlayerController Script-
using UnityEngine;
using System.Collections;
[RequireComponent (typeof (CharacterController))]
public class PlayerController : MonoBehaviour {
public float rotationSpeed = 450;
public float walkSpeed = 5;
public float runSpeed = 8;
private float acceleration = 5;
private Vector3 currentVelocityMod;
private CharacterController controller;
public Gun gun;
private Quaternion targetRotation;
private Camera cam;
void Start () {
controller = GetComponent<CharacterController>();
cam = Camera.main;
}
void Update () {
ControlMOUSE ();
//ControlWASD ();
if (Input.GetButtonDown ("Shoot")) {
gun.Shoot ();
}
else if (Input.GetButton ("Shoot"))
{
gun.ShootContinuous ();
}
}
void ControlMOUSE(){
Vector3 mousePos = Input.mousePosition;
mousePos = cam.ScreenToWorldPoint (new Vector3 (mousePos.x, mousePos.y, cam.transform.position.y - transform.position.y));
targetRotation = Quaternion.LookRotation (mousePos - new Vector3 (transform.position.x, 0, transform.position.z));
transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetRotation.eulerAngles.y, rotationSpeed * Time.deltaTime);
Vector3 input = new Vector3 (Input.GetAxisRaw("Horizontal"),0,Input.GetAxisRaw("Vertical"));
if (input != Vector3.zero) {
transform.rotation = Quaternion.LookRotation (input);
transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetRotation.eulerAngles.y, rotationSpeed * Time.deltaTime);
}
currentVelocityMod = Vector3.MoveTowards (currentVelocityMod, input, acceleration * Time.deltaTime);
Vector3 motion = currentVelocityMod;
motion *= (Mathf.Abs (input.x) == 1 && Mathf.Abs(input.z) == 1)?.7f:1;
motion *= (Input.GetButton("Run"))?runSpeed:walkSpeed;
motion += Vector3.up * -8;
controller.Move (motion * Time.deltaTime);
}
void ControlWASD()
{
Vector3 input = new Vector3 (Input.GetAxisRaw("Horizontal"),0,Input.GetAxisRaw("Vertical"));
if (input != Vector3.zero) {
transform.rotation = Quaternion.LookRotation (input);
transform.eulerAngles = Vector3.up * Mathf.MoveTowardsAngle(transform.eulerAngles.y, targetRotation.eulerAngles.y, rotationSpeed * Time.deltaTime);
}
currentVelocityMod = Vector3.MoveTowards (currentVelocityMod, input, acceleration * Time.deltaTime);
Vector3 motion = currentVelocityMod;
motion *= (Mathf.Abs (input.x) == 1 && Mathf.Abs(input.z) == 1)?.7f:1;
motion *= (Input.GetButton("Run"))?runSpeed:walkSpeed;
motion += Vector3.up * -8;
controller.Move (motion * Time.deltaTime);
}
}
Any help would be greatly appreciated thanks