Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Begginner problem Help needed FPS Sniper scope issues

Discussion in 'Scripting' started by FirstBB8Droid, May 30, 2020.

  1. FirstBB8Droid

    FirstBB8Droid

    Joined:
    May 17, 2020
    Posts:
    39
    I'm working on an FPS right now and I'm trying to create a sniper with a scope, when I would switch weapons while scoped in I would stay scoped in so I'm trying to fix it. I made some code to check if I'm scoped in when switching weapons and to have it so then if you are zoomed in you no longer will be. I am now getting an error message saying
    NullReferenceException: Object reference not set to an instance of an object
    WeaponSwitching.Update () (at Assets/scripts/WeaponSwitching.cs:31)
    Here's my Weapon switching code
    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class WeaponSwitching : MonoBehaviour{
    4.  
    5.     public int selectedWeapon = 0;
    6.     public scope Scope;
    7.  
    8.     // Start is called before the first frame update
    9.     void Start() {
    10.         SelectWeapon();
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update(){
    15.  
    16.         int previousSelectedWeapon = selectedWeapon;
    17.  
    18.         if (Input.GetAxis("Mouse ScrollWheel") > 0f)
    19.         {
    20.             if (Scope.isScoped == true)
    21.             {
    22.                 Scope.isScoped = false;
    23.             }
    24.             if (selectedWeapon >= transform.childCount - 1)
    25.                 selectedWeapon = 0;
    26.             else
    27.                 selectedWeapon++;
    28.         }
    29.         if(Input.GetAxis("Mouse ScrollWheel") < 0f)
    30.         {
    31.             if(Scope.isScoped==true)
    32.             {
    33.                 Scope.isScoped = false;
    34.             }
    35.             if (selectedWeapon <= 0)
    36.                 selectedWeapon = transform.childCount - 1;
    37.             else
    38.                 selectedWeapon--;
    39.  
    40.         }
    41.         if (Input.GetKeyDown(KeyCode.Alpha1))
    42.         {
    43.             selectedWeapon = 0;
    44.         }
    45.         if (Input.GetKeyDown(KeyCode.Alpha2))
    46.         {
    47.             selectedWeapon = 1;
    48.         }
    49.         if (Input.GetKeyDown(KeyCode.Alpha3))
    50.         {
    51.             selectedWeapon = 2;
    52.         }
    53.         if (Input.GetKeyDown(KeyCode.Alpha4))
    54.         {
    55.             selectedWeapon = 3;
    56.         }
    57.         if (previousSelectedWeapon != selectedWeapon)
    58.         {
    59.             SelectWeapon();
    60.         }
    61.     }
    62.     void SelectWeapon()
    63.     {
    64.         int i = 0;
    65.         foreach (Transform weapon in transform)
    66.         {
    67.             if (i == selectedWeapon)
    68.                 weapon.gameObject.SetActive(true);
    69.             else
    70.                 weapon.gameObject.SetActive(false);
    71.             i++;
    72.         }
    73.     }
    74. }
    75.  
    Here's my scope code
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class scope : MonoBehaviour
    6. {
    7.     public Animator animator;
    8.  
    9.     public GameObject scopeOverlay;
    10.     public GameObject weaponCamera;
    11.     public Camera mainCamera;
    12.     public float scopedFOV = 0.25f;
    13.     private float preFOV;
    14.     public MouseLook mouseControll;
    15.     public float ScopedSensitivity = 25f;
    16.     public bool isScoped = false;
    17.  
    18.     void Update()
    19.     {
    20.         if (Input.GetButtonDown("Fire3"))
    21.         {
    22.             isScoped = !isScoped;
    23.             animator.SetBool("scoped", isScoped);
    24.  
    25.             if (isScoped)
    26.                 StartCoroutine(OnScoped());
    27.             else
    28.                 OnUnscoped();
    29.         }
    30.     }
    31.     void OnUnscoped()
    32.     {
    33.         scopeOverlay.SetActive(false);
    34.         weaponCamera.SetActive(true);
    35.         mainCamera.fieldOfView = preFOV;
    36.         mouseControll.mouseSensitivity = 100;
    37.     }
    38.        
    39.  
    40.     IEnumerator OnScoped()
    41.     {
    42.         yield return new WaitForSeconds(.25f);
    43.         weaponCamera.SetActive(false);
    44.         scopeOverlay.SetActive(true);
    45.  
    46.         preFOV = mainCamera.fieldOfView;
    47.         mainCamera.fieldOfView = scopedFOV;
    48.        
    49.     mouseControll.mouseSensitivity = ScopedSensitivity;
    50.     }
    51. }
    52.  
    and Finally here's my Mouse Look code (Don't know if you need it, I just reference it in the scope code)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class MouseLook : MonoBehaviour
    6. {
    7.  
    8.     public float mouseSensitivity = 100f;
    9.  
    10.     public Transform playerBody;
    11.  
    12.     float xRotation = 0f;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.         Cursor.lockState = CursorLockMode.Locked;
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
    24.         float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
    25.  
    26.         xRotation -= mouseY;
    27.         xRotation = Mathf.Clamp(xRotation, -90f, 90f);
    28.  
    29.         transform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
    30.         playerBody.Rotate(Vector3.up * mouseX);
    31.     }
    32. }
    33.  
    if you need any other code just ask and I'll share it with you
     
  2. FirstBB8Droid

    FirstBB8Droid

    Joined:
    May 17, 2020
    Posts:
    39
    I also want to say incase it helps that I'm using a tutorial by brackeys, here's the link
     
  3. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    NullReferenceException: Object reference not set to an instance of an object
    WeaponSwitching.Update () (at Assets/scripts/WeaponSwitching.cs:31)

    have you set scope?from editor?

    and this set the bool but the logic in scope only call when you get fire button down
    Code (CSharp):
    1. if(Scope.isScoped==true)
    2.             {
    3.                 Scope.isScoped = false;
    4.             }
    Code (CSharp):
    1. if (Input.GetButtonDown("Fire3"))
    2.         {
    3.             isScoped = !isScoped;
    4.             animator.SetBool("scoped", isScoped);
    5.             if (isScoped)
    6.                 StartCoroutine(OnScoped());
    7.             else
    8.                 OnUnscoped();
    9.         }
    if you switch weapon and didnt press fire the scope will remain onscoped

    Code (CSharp):
    1. if (isScoped)
    2.                 StartCoroutine(OnScoped());
    3.             else
    4.                 OnUnscoped();
    this need to be outside on of getbuttondown
     
    FirstBB8Droid likes this.
  4. FirstBB8Droid

    FirstBB8Droid

    Joined:
    May 17, 2020
    Posts:
    39
     
  5. Yanne065

    Yanne065

    Joined:
    Feb 24, 2018
    Posts:
    175
    happy to help