Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Trouble creating a class for stats

Discussion in 'Scripting' started by Eliotz, May 29, 2016.

  1. Eliotz

    Eliotz

    Joined:
    Jul 18, 2015
    Posts:
    81
    So basically I decided it will be a good Idea to store all my weapon stats into a class (WeaponStats) so I don't have to pass like 5 or 6 floats in a function that can later set those stats to the bullet. But in my WeaponProperties() Method all of them gives me null Reference Exception errors and I cant figure out why.

    Like when I try to extract the information from these classes that's when it occurs and it only shows up when I press play.

    Here are the scrips:
    I still haven't finished writing them, so there will be some unused variables, but everything works perfectly except for the WeaponProperties() part.

    WeaponLibary.cs

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class WeaponLibary : MonoBehaviour {
    5.  
    6.     public class WeaponStats
    7.     {
    8.         public float fireRate;
    9.         public float damage;
    10.         public float velocity;
    11.         public float accuracy;
    12.         public float range;
    13.  
    14.         public WeaponStats (float fr, float dmg, float v, float acc, float rng)
    15.         {
    16.             fireRate = fr;
    17.             damage = dmg;
    18.             velocity = v;
    19.             accuracy = acc;
    20.             range = rng;
    21.         }
    22.     }
    23.     //Variables
    24.     public Weapon weaponSlot;
    25.     public Weapon weaponSlot2;
    26.  
    27.     GunController cg;
    28.  
    29.     WeaponStats wepStats;
    30.     WeaponStats wepStats2;
    31.     Weapon weapon;
    32.     //Assigns the stats for the weapons in the slots
    33.     void Start()
    34.     {
    35.         cg = GameObject.FindGameObjectWithTag("Player").GetComponent<GunController>();
    36.         AssignWeapons(wepStats, weaponSlot, wepStats2, weaponSlot2);
    37.     }
    38.     //Figures out what stats does the weapons in the Slots have
    39.     void Libary()
    40.     {
    41.         switch (weaponSlot.id)
    42.         {
    43.             case "Pistol":
    44.                 wepStats = new WeaponStats(300, 5, 20, 10, 10);
    45.                 break;
    46.             case "Rifle":
    47.                 wepStats = new WeaponStats(125, 10, 22, 12, 20);
    48.                 break;
    49.             case "Uzi":
    50.                 wepStats = new WeaponStats(100, 3, 20, 7, 10);
    51.                 break;
    52.         }
    53.         switch (weaponSlot2.id)
    54.         {
    55.             case "Pistol":
    56.                 wepStats2 = new WeaponStats(300, 5, 20, 10, 10);
    57.                 break;
    58.             case "Rifle":
    59.                 wepStats2 = new WeaponStats(125, 10, 22, 12, 20);
    60.                 break;
    61.             case "Uzi":
    62.                 wepStats2 = new WeaponStats(100, 3, 20, 7, 10);
    63.                 break;
    64.         }
    65.     }
    66.     //Passes all the weapons with their stats to GunController
    67.     void AssignWeapons(WeaponStats wStat1, Weapon wep, WeaponStats wStat2, Weapon wep2)
    68.     {
    69.         cg.WeaponFeeder(wStat1, wep, wStat2, wep2);
    70.     }
    71. }
    GunController.cs

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GunController : MonoBehaviour {
    5.     [Range(0f, 1000f)]
    6.     public float fireRate;
    7.     float damage;
    8.     float velocity;
    9.  
    10.     WeaponLibary.WeaponStats wepStats;
    11.     WeaponLibary.WeaponStats primaryStats;
    12.     WeaponLibary.WeaponStats secondaryStats;
    13.  
    14.     public Transform hands;
    15.     public Transform holster1;
    16.     public Transform holster2;
    17.  
    18.     public Weapon startingWeapon;
    19.     public Vector3 startingStats;
    20.     Weapon primaryWeapon;
    21.     Weapon secondaryWeapon;
    22.     Weapon gunInHands;
    23.     Weapon inHolster;
    24.  
    25.  
    26.     //Sets the starting weapon
    27.     void Start()
    28.     {
    29.  
    30.     }
    31.  
    32.     //Assigns weapons to their slots
    33.     public void WeaponFeeder(WeaponLibary.WeaponStats _primaryStats, Weapon _primary, WeaponLibary.WeaponStats _secondaryStats, Weapon _secondary)
    34.     {
    35.         Weapon primaryWeapon = _primary;
    36.         Weapon secondaryWeapon = _secondary;
    37.         WeaponLibary.WeaponStats primaryStats = _primaryStats;
    38.         WeaponLibary.WeaponStats secondaryStats = _secondaryStats;
    39.         EquipWeapon(secondaryWeapon, primaryWeapon, primaryStats);
    40.     }
    41.     //Equips the weapon
    42.     public void EquipWeapon (Weapon secWeapon, Weapon weapon, WeaponLibary.WeaponStats _weaponStats)
    43.     {
    44.         if (inHolster != null)
    45.         {
    46.             Destroy(inHolster.gameObject);
    47.         }
    48.         inHolster = Instantiate(secWeapon, holster2.position, holster2.rotation) as Weapon;
    49.         inHolster.transform.parent = holster2;
    50.         if(gunInHands != null)
    51.         {
    52.             Destroy(gunInHands.gameObject);
    53.         }
    54.         gunInHands = Instantiate(weapon, hands.position, hands.rotation) as Weapon;
    55.         gunInHands.transform.parent = hands;
    56.         WeaponProperties();
    57.     }
    58.     //Equiped weapon properties
    59.     void WeaponProperties()
    60.     {
    61.         fireRate = primaryStats.fireRate;
    62.         damage = primaryStats.damage;
    63.         velocity = primaryStats.velocity;  
    64.     }
    65.  
    66.     void Update()
    67.     {
    68.         //Shoot
    69.         if (Input.GetMouseButton(0) && gunInHands != null)
    70.         {
    71.             gunInHands.Fire(fireRate, damage, velocity);
    72.         }
    73.     }
    74. }
     
  2. Thomas-Mountainborn

    Thomas-Mountainborn

    Joined:
    Jun 11, 2015
    Posts:
    489
    If you get a null reference, that's simply because the variables haven't been assigned a value. When you take a look at the WeaponFeeder() method, you will see that you assign values to local variables "primaryStats" etc, not the field variables of the same name.

    This is an easy mistake to make, since you got your C# naming convention all jumbled up. Private field variables start with an underscore, local variables and parameters just start with a lower case letter - you got it exactly the wrong way round. Making private field variables start with an underscore (and public ones with a capital) makes it very easy to see inside a method that these aren't local variables, and whose values are therefore not just defined inside the method itself. If you make local variables start with an underscore, you're just adding to the confusion.
     
  3. Eliotz

    Eliotz

    Joined:
    Jul 18, 2015
    Posts:
    81
    Oooh Yeah I get it now, I made a useless local variable that I never used and assigned the stats to that variable while using a that empty public variable in the WeaponProperties() method.

    I fixed it all and it works fine now, Thanks bro!
     
  4. Eliotz

    Eliotz

    Joined:
    Jul 18, 2015
    Posts:
    81
    Oh no sorry that wasn't a public variable but whatever.