Search Unity

Problems with Aiming code/script for fps.

Discussion in 'Scripting' started by egeqgeg, Jan 9, 2022.

  1. egeqgeg

    egeqgeg

    Joined:
    May 26, 2021
    Posts:
    3
    I have been using Unity for a few months, and have been trying to make an fps. I have been able to resolve most of my code errors, but this one has me stumped. The script below is meant to manage the player's weapons. It gives me an error when I press right click to aim. The bolded section is the part of the script related to aiming.



    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6.  
    7. namespace Mechagame
    8. {
    9.     public class weapon : MonoBehaviour
    10.     {
    11.         public gun[] loadout;
    12.         public Transform weaponParent;
    13.         private int currentindex;
    14.  
    15.  
    16.  
    17.         private GameObject currentWeapon;
    18.         // Start is called before the first frame update
    19.         void Start()
    20.         {
    21.  
    22.         }
    23.  
    24.          //Update is called once per frame
    25.         void Update()
    26.         {
    27.             if (Input.GetKeyDown(KeyCode.Alpha1)) Equip(0);
    28.  
    29.  
    30.  
    31. [B]
    32. [COLOR=#ff4d4d]            if (currentWeapon != null && Input.GetMouseButton(1))
    33.             {
    34.                 aim(true);
    35.             }[/COLOR][/B]
    36.  
    37.         }
    38.  
    39.         void Equip(int p_ind)
    40.         {
    41.             if (currentWeapon != null) Destroy(currentWeapon);
    42.  
    43.             currentindex = p_ind;
    44.  
    45.             GameObject t_newEquipment = Instantiate(
    46.                 loadout[p_ind].prefab,
    47.                 weaponParent.position,
    48.                 weaponParent.rotation,
    49.                 weaponParent) as GameObject;
    50.             t_newEquipment.transform.localPosition = Vector3.zero;
    51.             t_newEquipment.transform.localEulerAngles = Vector3.zero;
    52.  
    53.             currentWeapon = t_newEquipment;
    54.         }
    55.  
    56. [COLOR=#ff4d4d]        [B]void aim(bool aiming)[/B][/COLOR]
    57. [B][COLOR=#ff4d4d]        {
    58.             Transform t_anchor = currentWeapon.transform.Find("firstpersplayer/item(1)/pistole/states/item");
    59.             Transform t_states_ads = currentWeapon.transform.Find("firstpersplayer/item(1)/pistole/states/ads");
    60.             Transform t_states_idle = currentWeapon.transform.Find("firstpersplayer/item(1)/pistole/states/idle");
    61.  
    62.             if (aiming != true)
    63.             {
    64.  
    65.                 t_anchor.position = Vector3.Lerp(t_anchor.position, t_states_idle.position, Time.deltaTime * loadout[currentindex].aimSpeed);
    66.  
    67.             }[/COLOR][/B]
    68.  
    69.  
    70.  
    71. The underlined section below is where I am getting errors.
    72.  
    73.     [B][COLOR=#ff4d4d]        else
    74.             {
    75. [/COLOR][/B]
    76. [U][B][COLOR=#4d4dff]                t_anchor.position = Vector3.Lerp(t_anchor.position, t_states_ads.position, Time.deltaTime * loadout[currentindex].aimSpeed);[/COLOR][/B][/U]
    77. [B][COLOR=#ff4d4d]
    78.             }
    79.         }
    80.  
    81.  
    82.  
    83.  
    84.  
    85.  
    86.     }
    87. }[/COLOR][/B]
    88.  
    89.  

    I know what the error means, I am just not sure as to why it keeps occurring. Can anyone please help me with this? Thanks in advance!
     
  2. egeqgeg

    egeqgeg

    Joined:
    May 26, 2021
    Posts:
    3
    sorry for the bad code input, I have not used the unity forum to post before. Reposted below is a clearer view of the code and the error.

    NullReferenceException: Object reference not set to an instance of an object
    Mechagame.weapon.aim (System.Boolean aiming) (at Assets/scripts/weapon.cs:71)
    Mechagame.weapon.Update () (at Assets/scripts/weapon.cs:33)


    Code (CSharp):
    1.  
    2.  
    3. using System.Collections;
    4. using System.Collections.Generic;
    5. using UnityEngine;
    6.  
    7.  
    8. namespace Mechagame
    9. {
    10.     public class weapon : MonoBehaviour
    11.     {
    12.         public gun[] loadout;
    13.         public Transform weaponParent;
    14.         private int currentindex;
    15.  
    16.  
    17.  
    18.         private GameObject currentWeapon;
    19.         // Start is called before the first frame update
    20.         void Start()
    21.         {
    22.  
    23.         }
    24.  
    25.          //Update is called once per frame
    26.         void Update()
    27.         {
    28.             if (Input.GetKeyDown(KeyCode.Alpha1)) Equip(0);
    29.  
    30.  
    31.  
    32.  
    33.             if (currentWeapon != null && Input.GetMouseButton(1))
    34.             {
    35.                 aim(true);
    36.             }
    37.  
    38.         }
    39.  
    40.         void Equip(int p_ind)
    41.         {
    42.             if (currentWeapon != null) Destroy(currentWeapon);
    43.  
    44.             currentindex = p_ind;
    45.  
    46.             GameObject t_newEquipment = Instantiate(
    47.                 loadout[p_ind].prefab,
    48.                 weaponParent.position,
    49.                 weaponParent.rotation,
    50.                 weaponParent) as GameObject;
    51.             t_newEquipment.transform.localPosition = Vector3.zero;
    52.             t_newEquipment.transform.localEulerAngles = Vector3.zero;
    53.  
    54.             currentWeapon = t_newEquipment;
    55.         }
    56.  
    57.         void aim(bool aiming)
    58.         {
    59.             Transform t_anchor = currentWeapon.transform.Find("firstpersplayer/item(1)/pistole/states/item");
    60.             Transform t_states_ads = currentWeapon.transform.Find("firstpersplayer/item(1)/pistole/states/ads");
    61.             Transform t_states_idle = currentWeapon.transform.Find("firstpersplayer/item(1)/pistole/states/idle");
    62.  
    63.             if (aiming != true)
    64.             {
    65.  
    66.                 t_anchor.position = Vector3.Lerp(t_anchor.position, t_states_idle.position, Time.deltaTime * loadout[currentindex].aimSpeed);
    67.  
    68.             }
    69.  
    70.  
    71.  
    72. // this is where the error starts; it is associated with the aim function
    73.  
    74.             else
    75.             {
    76.  
    77.                 t_anchor.position = Vector3.Lerp(t_anchor.position, t_states_ads.position, Time.deltaTime * loadout[currentindex].aimSpeed);
    78.  
    79.             }
    80.         }
    81.  
    82.  
    83.  
    84.  
    85.  
    86.  
    87.     }
    88. }
    89.  
    90.  
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Fortunately it doesn't even matter what you're doing when you get this.

    The answer is always the same... ALWAYS. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:

    - drag it in using the inspector
    - code inside this script initializes it
    - some OTHER external code initializes it
    - ? something else?

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.

    Here is a clean analogy of the actual underlying problem of a null reference exception:

    https://forum.unity.com/threads/nul...n-instance-of-an-object.1108865/#post-7137032
     
    D12294 likes this.
  4. egeqgeg

    egeqgeg

    Joined:
    May 26, 2021
    Posts:
    3
    Thanks so much! I finally managed to find where the error was coming from. I had accidentally closed a script in the editor and forgot how to open it.
     
    Last edited: Jan 9, 2022