Object reference not set to an instance of an object

Updated on April 15, 2018 in  [R] Scripts
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
7 on April 1, 2018

I have two scripts, one for shooting on the gun and another one for controlling the player on the player, Shooting script is a MonoBehaviour, while PlayerController is NetworkBehaviour and what it does is it takes the Shoot() method from Shooting and makes it into a [Command] CmdShoot() and this is an error. The reason why I can’t merge these two scripts because later I plan to add more than one gun, so each gun would have this script on it

Shooting:

using UnityEngine;
public class Shooting : MonoBehaviour {
public static Shooting ShootingScript;
PlayerWeapon weapon;
 PlayerController PlayerScript;
public Transform gunEnd;
 public LineRenderer gunLine;
 RaycastHit shootHit;
void Awake()
 {
 gunLine = GetComponent<LineRenderer>();
 gunLine.enabled = false;
 PlayerScript = GetComponent<PlayerController>();
 }
public void Shoot()
 {
 gunLine.enabled = true;
 gunLine.SetPosition(0, gunEnd.position);
if (Physics.Raycast(gunEnd.position, gunEnd.transform.forward, out shootHit, weapon.range))
 {
 if (shootHit.collider.tag == PlayerScript.playerTag)
 {
 PlayerScript.playerHealth -= weapon.damage;
 }
 gunLine.SetPosition(1, shootHit.point);
 }
 else
 {
 gunLine.SetPosition(1, gunEnd.position + gunEnd.transform.forward * weapon.range);
 }
 }
}

PlayerController:

using UnityEngine;
using UnityEngine.Networking;
public class PlayerController : NetworkBehaviour{
public static PlayerController PlayerScript;
Shooting ShootingScript;
public string playerTag = "Player";
 public float movementSpeed = 10f;
 public float playerHealth = 100f;
 public Rigidbody PlayerRB;
 public Transform CameraTR;
void Awake()
 {
 ShootingScript = GetComponent<Shooting>();
 }
void Start()
 {
 CameraTR = GameObject.FindGameObjectWithTag("MainCamera").GetComponent<Transform>();
 PlayerRB = gameObject.GetComponent<Rigidbody>();
 PlayerRB.constraints = RigidbodyConstraints.FreezeRotation;
 }
void Update()
 {
 if(Input.GetButtonDown("Fire1"))
 {
 CmdShoot();
 }
 }
void FixedUpdate()
 {
 if (Input.GetKey(KeyCode.W))
 {
 transform.position += transform.forward * Time.deltaTime * movementSpeed;
 }
 }
void OnCollisionExit(Collision collision)
 {
 PlayerRB.velocity = Vector3.zero;
 }
void LateUpdate()
 {
 CameraTR.position = PlayerRB.position + new Vector3(0, 14, -6);
 }
[Command]
 void CmdShoot()
 {
 ShootingScript.Shoot(); //This is where the error happens
 }
}

Error message:

NullReferenceException: Object reference not set to an instance of an object
PlayerController.CmdShoot () (at Assets/Scripts/PlayerController.cs:57)
PlayerController.CallCmdShoot ()
PlayerController.Update () (at Assets/Scripts/PlayerController.cs:32)

  • Liked by
Reply
2 on April 2, 2018

Does you player have a LineRenderer component on them?

on April 6, 2018

Tried it, it doesn’t work

Wise
on April 6, 2018

What doesn’t work?

You need a LineRenderer on the same object you have Shooting on. If you don’t,  GetComponent will give you a null reference.

 

Also, the error messages tell you exactly where the null reference is coming from.

They’re both in PlayerController, lines 57, and 32.

Show more replies
  • Liked by
Reply
Cancel
3 on April 15, 2018

So this is an updated script after a few days and now I get the same error on gunEndV3 in Awake function

PlayerWeapon playerWeapon;
 public float timeBetweenBullets = 0.15f;
 public Transform gunEnd;
 Vector3 gunEndV3;
public float timer; 
 Ray shootRay; 
 RaycastHit shootHit; 
 int shootableMask; 
 public LineRenderer gunLine; 
 public float effectsDisplayTime = 0.2f;
void Awake()
 {
 shootableMask = LayerMask.GetMask("RemotePlayer");
 gunLine = GetComponent<LineRenderer>();
 playerWeapon = new PlayerWeapon();
 gunEndV3 = gunEnd.transform.position; //This is the error
 }
void Update()
 {
 timer += Time.deltaTime;
if (timer >= timeBetweenBullets * effectsDisplayTime)
 {
 gunLine.enabled = false;
 }
 }
public void Shoot()
 {
 timer = 0f;
gunLine.enabled = true;
 gunLine.SetPosition(0, gunEndV3); /* I want line renderer to start at the gun end, 
                                                          but I need to convert Transform to Vector3 */
shootRay.origin = gunEndV3;
 shootRay.direction = transform.forward;
if (Physics.Raycast(shootRay, out shootHit, playerWeapon.range, shootableMask))
 {
 gunLine.SetPosition(1, shootHit.point);
 }
 else
 {
 gunLine.SetPosition(1, shootRay.origin + shootRay.direction * playerWeapon.range);
 }
 }
Wise
on April 15, 2018

Do you assign gunEnd in the inspector?

on April 15, 2018

Yes

Wise
on April 15, 2018

Then could you post a screenshot of the error message?

And a screenshot of your code so i can see the line numbers.

 

Edit: real quick though, add

if(gunEndV3 == null) return;

to the beginning of your Shoot() method.

 

also, why do you need the extra Vector3? Why don’t you just use gunEnd.transform.position instead?

Show more replies
  • Liked by
Reply
Cancel