I am making an FPS game and I am still in VERY early stages. The way I have shooting is with Raycasts and there is another way I can make Raycast shooting, but if I do it a different way, it would be a little hard to emulate bullet tracers and if the gun would be through a wall, then the player could still shoot and kill something which is bad. I am going to copy and paste my whole script. The error is on line 142. I do not know why it is giving me this error because I don’t know what to reference. One more thing, this is what the error says EXACTLY:
"NullReferenceException: Object reference not set to an instance of an object
WeaponsSystem.AssignTransform () (at Assets/Scripts/Player Scripts/Combat Scripts/WeaponsSystem.cs:142)
WeaponsSystem.Update () (at Assets/Scripts/Player Scripts/Combat Scripts/WeaponsSystem.cs:35)"
Please help me!
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class WeaponsSystem : MonoBehaviour
{
//Class References
public EconomySystem economySystem;
public WeaponType weaponType = new WeaponType (0.0f, 0.0f, 0, 0.0f, 0.0f, "", "", "");
//Shooting Variables
public Transform bloodDecal;
private float nextFire = 0.0f;
void Start ()
{
//{TEMP} This assigns Combat Knife as weapon player is using. This is {TEMP}
//weaponType = new WeaponType (0.3f, 10.0f, 0, 10.0f, 1.5f, "Melee", "Melee", "Combat Knife");
weaponType = new WeaponType (0.3f, 10.0f, 30, 10.0f, 25.0f, "Fully-Automatic", "Assault Rifle", "M4A1");
weaponType.weaponAmmoMags[0] = 10;
print ("Fire Rate: " + weaponType.weaponFireRate + "\n" +
"Damage: " + weaponType.weaponDamage + "\n" +
"Accuracy: " + weaponType.weaponAccuracy + "\n" +
"Shoot Type: " + weaponType.weaponShootType + "\n" +
"Gun Type: " + weaponType.weaponType + "\n" +
"Gun Name: " + weaponType.weaponName);
}
void Update ()
{
WeaponFireRate ();
CheckAmmo ();
AssignTransform ();
}
void CheckAmmo ()
{
if (Input.GetKeyDown (KeyCode.R))
{
//**Assault Rifle Ammo Check**
if (weaponType.weaponType == "Assault Rifle" && weaponType.weaponAmmoInMag >= 0 && weaponType.weaponAmmoInMag < weaponType.weaponAmmoAllowedInMag && weaponType.weaponAmmoMags[0] >= 1)
{
ReloadWeapon ("Assault Rifle");
}
}
}
void ReloadWeapon (string WeaponType)
{
//**Assault Rifle Reloading**
if (WeaponType == "Assault Rifle")
{
if (weaponType.weaponName == "M4A1")
{
weaponType.weaponAmmoInMag = weaponType.weaponAmmoAllowedInMag;
weaponType.weaponAmmoMags[0]--;
print ("Ammo in Magazine: " + weaponType.weaponAmmoInMag + "\n" +
"Ammo Magazine(s): " + weaponType.weaponAmmoMags[0]);
}
}
}
void WeaponFireRate ()
{
//Makes sure that a Melee type of weapon isn't Fully-Automatic
if (weaponType.weaponShootType == "Melee")
{
if (Input.GetButtonDown ("Fire1") && Time.time > nextFire)
{
nextFire = Time.time + weaponType.weaponFireRate;
FireWeapon ();
}
}
//Makes sure that a Semi-Automatic, Bolt-Action, or Pump-Action type of weapon isn't Fully-Automatic
else if (weaponType.weaponShootType == "Semi-Automatic" || weaponType.weaponShootType == "Bolt-Action" || weaponType.weaponShootType == "Pump-Action")
{
if (Input.GetButtonDown ("Fire1") && Time.time > nextFire && weaponType.weaponAmmoInMag >= 1)
{
weaponType.weaponAmmoInMag--;
nextFire = Time.time + weaponType.weaponFireRate;
FireWeapon ();
}
else if (Input.GetButtonDown ("Fire1") && Time.time > nextFire && weaponType.weaponAmmoInMag == 0)
{
print ("Semi-Automatic, Bolt-Action, or Pump-Action Weapon out of ammo in magazine. Reload!");
}
}
//Makes sure a Fully-Automatic weapon is Fully-Automatic, not anything else
else if (weaponType.weaponShootType == "Fully-Automatic")
{
if (Input.GetButton ("Fire1") && Time.time > nextFire && weaponType.weaponAmmoInMag >= 1)
{
weaponType.weaponAmmoInMag--;
nextFire = Time.time + weaponType.weaponFireRate;
FireWeapon ();
}
else if (Input.GetButton ("Fire1") && Time.time > nextFire && weaponType.weaponAmmoInMag == 0)
{
print ("Fully-Automatic Weapon out of ammo in magazine. Reload!");
}
}
//Spits out an error if gun isn't Melee, Semi-Automatic, Fully-Automatic, Bolt-Action, Pump-Action
else
{
Debug.LogError ("Something has gone wrong in the 'WeaponsSytem.cs' script." + "\n" +
"Restart the game and if this error still shows, check the 'WeaponsSystem.cs' script." + "\n" +
"One of the error(s) that may occur could be a spelling for the 'weaponType' variable.");
}
}
void FireWeapon ()
{
//Raycasting Information
//Ray ray = Camera.main.ScreenPointToRay (new Vector3(Screen.width / 2.0f, Screen.height / 2.0f, 0.0f)); {OTHER RAYCAST METHOD, DON'T WANT TO USE IF I DON'T HAVE TO}
Ray ray = new Ray (weaponType.weaponTransform.position, weaponType.weaponTransform.forward);
RaycastHit hitInfo = new RaycastHit ();
//Checks to see if Raycast hits something
if (Physics.Raycast (ray, out hitInfo, weaponType.weaponHitRange))
{
print ("Object Weapon just hit: " + hitInfo.collider.tag);
if (hitInfo.collider.tag == "Common Zombie")
{
//Adds Blood Decal to zombie when hit
Transform bloodDecalClone = (Transform)Instantiate (bloodDecal, hitInfo.point, Quaternion.identity);
Destroy (bloodDecalClone.gameObject, 0.2f);
//Adds 10 ZomB Cash
economySystem.ZomBCash += 10;
//Sends Messsage to the zombie
hitInfo.collider.SendMessage ("ApplyDamageToZombie", weaponType.weaponDamage, SendMessageOptions.DontRequireReceiver);
}
}
}
void AssignTransform ()
{
weaponType.weaponTransform = GameObject.Find (weaponType.weaponName).transform;
print (weaponType.weaponTransform); //FOR DEBUGGING ONLY
}
}
public class WeaponType
{
public float weaponFireRate = 0.0f;
public float weaponDamage = 0.0f;
public int[] weaponAmmoMags = new int[5];
/*
* weaponDamge Array Indexs
* [0]: "Assault Rifle" Ammo Mags,
* [1]: "Sub-Machine Gun" Ammo Mags,
* [2]: "Handgun" Ammo Mags,
* [3]: "Sniper Rifle" Ammo Mags,
* [4]: "Shotgun" Ammo Mags
*/
public int weaponAmmoInMag = 0;
public int weaponAmmoAllowedInMag = 0;
public float weaponAccuracy = 0.0f;
public float weaponHitRange = 0.0f;
public string weaponShootType;
/*
* TYPES OF SHOOT TYPES TO CHOOSE FROM: (8 in all)
*
* FOR MELEE:
* "Melee"
*
* FOR ASSAULT RIFLES AND SUB-MACHINE GUNS:
* "Fully-Automatic",
* "Semi-Automatic"
*
* FOR HANDGUNS:
* "Semi-Automatic" (ALWAYS CHOOSE THIS FOR HANDGUNS)
*
* FOR SNIPER RIFLES:
* "Bolt-Action",
* "Semi-Automatic"
*
* FOR SHOTGUNS:
* "Pump-Action",
* "Semi-Automatic",
* "Fully-Automatic"
*/
public string weaponType;
/*
* TYPES OF GUNS TO CHOOSE FROM:
*
* "Melee"
* "Assault Rifle",
* "Sub-Machine Gun",
* "Handgun",
* "Sniper Rifle",
* "Shotgun"
*/
public string weaponName;
public Transform weaponTransform;
public WeaponType (float FireRate, float Damage, int AmmoAllowedInMag, float Accuracy, float HitRange, string ShootType, string WeaponType, string Name)
{
weaponFireRate = FireRate;
weaponDamage = Damage;
weaponAmmoAllowedInMag = AmmoAllowedInMag;
weaponAccuracy = Accuracy;
weaponHitRange = HitRange;
weaponShootType = ShootType;
weaponType = WeaponType;
weaponName = Name;
}
}