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
using UnityEngine;
using System.Collections;
public class WeaponLibary : MonoBehaviour {
public class WeaponStats
{
public float fireRate;
public float damage;
public float velocity;
public float accuracy;
public float range;
public WeaponStats (float fr, float dmg, float v, float acc, float rng)
{
fireRate = fr;
damage = dmg;
velocity = v;
accuracy = acc;
range = rng;
}
}
//Variables
public Weapon weaponSlot;
public Weapon weaponSlot2;
GunController cg;
WeaponStats wepStats;
WeaponStats wepStats2;
Weapon weapon;
//Assigns the stats for the weapons in the slots
void Start()
{
cg = GameObject.FindGameObjectWithTag("Player").GetComponent<GunController>();
AssignWeapons(wepStats, weaponSlot, wepStats2, weaponSlot2);
}
//Figures out what stats does the weapons in the Slots have
void Libary()
{
switch (weaponSlot.id)
{
case "Pistol":
wepStats = new WeaponStats(300, 5, 20, 10, 10);
break;
case "Rifle":
wepStats = new WeaponStats(125, 10, 22, 12, 20);
break;
case "Uzi":
wepStats = new WeaponStats(100, 3, 20, 7, 10);
break;
}
switch (weaponSlot2.id)
{
case "Pistol":
wepStats2 = new WeaponStats(300, 5, 20, 10, 10);
break;
case "Rifle":
wepStats2 = new WeaponStats(125, 10, 22, 12, 20);
break;
case "Uzi":
wepStats2 = new WeaponStats(100, 3, 20, 7, 10);
break;
}
}
//Passes all the weapons with their stats to GunController
void AssignWeapons(WeaponStats wStat1, Weapon wep, WeaponStats wStat2, Weapon wep2)
{
cg.WeaponFeeder(wStat1, wep, wStat2, wep2);
}
}
GunController.cs
using UnityEngine;
using System.Collections;
public class GunController : MonoBehaviour {
[Range(0f, 1000f)]
public float fireRate;
float damage;
float velocity;
WeaponLibary.WeaponStats wepStats;
WeaponLibary.WeaponStats primaryStats;
WeaponLibary.WeaponStats secondaryStats;
public Transform hands;
public Transform holster1;
public Transform holster2;
public Weapon startingWeapon;
public Vector3 startingStats;
Weapon primaryWeapon;
Weapon secondaryWeapon;
Weapon gunInHands;
Weapon inHolster;
//Sets the starting weapon
void Start()
{
}
//Assigns weapons to their slots
public void WeaponFeeder(WeaponLibary.WeaponStats _primaryStats, Weapon _primary, WeaponLibary.WeaponStats _secondaryStats, Weapon _secondary)
{
Weapon primaryWeapon = _primary;
Weapon secondaryWeapon = _secondary;
WeaponLibary.WeaponStats primaryStats = _primaryStats;
WeaponLibary.WeaponStats secondaryStats = _secondaryStats;
EquipWeapon(secondaryWeapon, primaryWeapon, primaryStats);
}
//Equips the weapon
public void EquipWeapon (Weapon secWeapon, Weapon weapon, WeaponLibary.WeaponStats _weaponStats)
{
if (inHolster != null)
{
Destroy(inHolster.gameObject);
}
inHolster = Instantiate(secWeapon, holster2.position, holster2.rotation) as Weapon;
inHolster.transform.parent = holster2;
if(gunInHands != null)
{
Destroy(gunInHands.gameObject);
}
gunInHands = Instantiate(weapon, hands.position, hands.rotation) as Weapon;
gunInHands.transform.parent = hands;
WeaponProperties();
}
//Equiped weapon properties
void WeaponProperties()
{
fireRate = primaryStats.fireRate;
damage = primaryStats.damage;
velocity = primaryStats.velocity;
}
void Update()
{
//Shoot
if (Input.GetMouseButton(0) && gunInHands != null)
{
gunInHands.Fire(fireRate, damage, velocity);
}
}
}