Search Unity

FPS Shooter Raycast Problem

Discussion in 'Scripting' started by Doomer022, Aug 30, 2018.

  1. Doomer022

    Doomer022

    Joined:
    May 16, 2018
    Posts:
    73
    Hey there! I am working on a multiplayer FPS shooter. I implemented 2 new weapons into my game. I test it, I can shoot, I can change weapons, but the weapons now dont do damage with the raycast. While ingame, with the scrollwheel, not just the weapon texture, but the name, range and damage also changes.

    In the script where it shoots out the raycast, it does nothing, it just puts out an error:

    "NullReferenceException: Object reference not set to an instance of an object"

    I dont know what I did, I didnt touch the code itself, it should still do the damage!

    Raycast shooting script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5. using UnityEngine.Audio;
    6.  
    7. public class PlayerShoot : NetworkBehaviour {
    8.  
    9.     private const string PLAYER_TAG = "Player";
    10.  
    11.     [SerializeField]
    12.     private PlayerWeapon weapon;
    13.  
    14.     [SerializeField]
    15.     private Camera cam;
    16.  
    17.     [SerializeField]
    18.     private LayerMask mask;
    19.  
    20.     [SerializeField]
    21.     private AudioSource weaponSFX;
    22.  
    23.     [SerializeField]
    24.     public ParticleSystem sniperMuzzle;
    25.  
    26.     [SerializeField]
    27.     public ParticleSystem shotgunMuzzle;
    28.  
    29.     [SerializeField]
    30.     public ParticleSystem rifleMuzzle;
    31.  
    32.     private PlayerWeapon currentWeapon;
    33.  
    34.     void Start()
    35.     {
    36.         if (cam == null)
    37.         {
    38.             Debug.LogError("No camera referenced");
    39.             this.enabled = false;
    40.         }
    41.     }
    42.     void Update()
    43.     {
    44.         if (Input.GetMouseButtonDown(0))
    45.         {
    46.             Shoot();
    47.         }
    48.     }
    49.  
    50.     [Client]
    51.     void Shoot()
    52.     {
    53.         RaycastHit hit;
    54.         weaponSFX.Play();
    55.         sniperMuzzle.Play();
    56.         shotgunMuzzle.Play();
    57.         rifleMuzzle.Play();
    58.         if (Physics.Raycast(cam.transform.position,cam.transform.forward,out hit, weapon.range, mask))
    59.         {
    60.             if (hit.collider.tag == PLAYER_TAG)
    61.             {
    62.                 CmdPlayerShot(hit.collider.name, weapon.damage);
    63.             }
    64.         }
    65.     }
    66.  
    67.     [Command]
    68.     void CmdPlayerShot(string _playerID, int _damage)
    69.     {
    70.         Debug.Log(_playerID + " has been shot!");
    71.  
    72.         Player _player = GameManager.GetPlayer(_playerID);
    73.         _player.RpcTakeDamage(_damage);
    74.     }
    75. }
    76.  
    My weapon switching code:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Networking;
    5.  
    6. [System.Serializable]
    7. public class PlayerWeapon : NetworkBehaviour {
    8.  
    9.     public string name = "SV-98";
    10.     public int damage = 35;
    11.     public int range = 50;
    12.  
    13.     [SyncVar]
    14.     private int slot = 3;
    15.  
    16.     [SerializeField]
    17.     private GameObject sniper;
    18.  
    19.     [SerializeField]
    20.     private GameObject shotgun;
    21.  
    22.     [SerializeField]
    23.     private GameObject rifle;
    24.  
    25.     private float mouseSWValue = 0f;
    26.  
    27.     void Update()
    28.     {
    29.         if (isLocalPlayer)
    30.         {
    31.             mouseSWValue = Input.GetAxis("Mouse ScrollWheel");
    32.  
    33.             if (mouseSWValue > 0f)
    34.             {
    35.                 slot = slot + 1;
    36.             }
    37.             if (mouseSWValue < 0f)
    38.             {
    39.                 slot = slot - 1;
    40.             }
    41.             if (slot > 3)
    42.             {
    43.                 slot = 3;
    44.             }
    45.             if (slot < 1)
    46.             {
    47.                 slot = 1;
    48.             }
    49.             if (slot == 1)
    50.             {
    51.                 sniper.SetActive(false);
    52.                 shotgun.SetActive(false);
    53.                 rifle.SetActive(true);
    54.                 name = "AKm72/2";
    55.                 damage = 15;
    56.                 range = 30;
    57.             }
    58.             if (slot == 2)
    59.             {
    60.                 sniper.SetActive(false);
    61.                 shotgun.SetActive(true);
    62.                 rifle.SetActive(false);
    63.                 name = "SPAS12";
    64.                 damage = 40;
    65.                 range = 10;
    66.             }
    67.             if (slot == 3)
    68.             {
    69.                 sniper.SetActive(true);
    70.                 shotgun.SetActive(false);
    71.                 rifle.SetActive(false);
    72.                 name = "SV-98";
    73.                 damage = 35;
    74.                 range = 50;
    75.             }
    76.         }
    77.     }
    78. }
    79.  
    Yes, I know, this is from one of Brackey's tutorials, but he stopped making that tutorial, so now I have to go on my own. So I just modified the "PlayerWeapon" script for my own liking! :)

    Thank you!
     
  2. Avo

    Avo

    Joined:
    Dec 4, 2010
    Posts:
    237
    What line number are you getting the null reference on?
     
  3. Doomer022

    Doomer022

    Joined:
    May 16, 2018
    Posts:
    73
    @Avo In the top script, line 58
     
  4. Avo

    Avo

    Joined:
    Dec 4, 2010
    Posts:
    237
    Try adding in two new lines right before the raycast and tell me what it gives you. Make sure you have the weapon and the camera set in the inspector. If you're trying it out in multiplayer make sure you rebuild and run the executable as well!

    Code (CSharp):
    1. Debug.Log("Weapon : " + weapon)
    2. Debug.Log("Camera : " + cam)
     
  5. Doomer022

    Doomer022

    Joined:
    May 16, 2018
    Posts:
    73
    @Avo Very interesting results!
    Weapon: (Nothing)
    Camera: Camera (UnityEngine.Camera)

    Sooo maybe because the weapon is nothing in the degub log, and its "PlayerWeapon weapon", maybe there is something wrong in the bottom script? But there are no errors there...
     
  6. Avo

    Avo

    Joined:
    Dec 4, 2010
    Posts:
    237
    Well, the null reference told us it had to be one of the two. Make sure your player weapon is set in the inspector for your PlayerShoot script. I suspect it got knocked loose somehow.

    if your player is a prefab make sure it's set from the prefabs weapon script.
     
  7. Doomer022

    Doomer022

    Joined:
    May 16, 2018
    Posts:
    73
    Okay, what Ill do now is fill the "PlayerWeapon" script with Debug.Logs and see where it flops. If I have a problem or found a solution, Ill report it to you here in the comments! Thanks for the help so far! :)
     
  8. Doomer022

    Doomer022

    Joined:
    May 16, 2018
    Posts:
    73
    Right, so the PlayerShoot script is missing the "Weapon", but I cant put anything into its place. It just writes this in the inspector:

    Weapon: None (Player Weapon)

    So what should I put there then? It wont allow any scripts nor gun textures.
     
  9. Doomer022

    Doomer022

    Joined:
    May 16, 2018
    Posts:
    73
    @Avo Okay, looks like I fixed it! Thank you for the help! Very Appreciated! :D
     
    Avo likes this.