Unexpected NullReferenceException
I am working on a game using a class library I made (Soldiers.dll) where I can place a SoldierObject script on a GameObject to make it behave like a soldier. However, I have had constant problems with an unexpected NullReferenceException and I quite frankly cannot understand what's going on here.
Here is the setup of my scene.
I have two GameObjects: Soldier1 and Soldier2 each having a SoldierObject script on them: The SoldierObject script looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Soldiers;
public class SoldierObject : MonoBehaviour
{
public WeaponObject weapon;
public ArmorObject armor;
public string Name = "Unknown";
public int Hp = 1;
public uint Strength = 0;
public uint Agility = 0;
public Soldier soldier;
void OnEnable()
{
soldier = new Soldier(Name, weapon.weapon, armor.armor, Hp, Strength, Agility);
}
void Update ()
{
Name = soldier.name();
weapon.weapon = soldier.wep();
armor.armor = soldier.arm();
Hp = soldier.hp();
Strength = soldier.strength();
Agility = soldier.agi();
}
}
So far everything works. However, things start going awry when the AttackOther script comes into play which is placed on Soldier1:
The script looks like this:
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using Soldiers;
public class AttackOther : MonoBehaviour {
public SoldierObject other;
public SoldierObject self;
private bool attacked = false;
// Update is called once per frame
void Update () {
if(Time.time >= 3 && !attacked)
{
attacked = true;
self.soldier.attack(other.soldier, 1);
}
}
}
The attack function is a public function of the Soldier class:
public SoldierState attack(Soldier other, double mult)
{
int atk = (int)Math.Round(this.wep().power() * mult) + (int)this.strength();
return other.hp((int)(other.hp() - Math.Abs(other.arm().defense() - atk))) > 0 ? other.state : SoldierState.dead;
}
At this time whenever
self.soldier.attack(other.soldier, 1);
is executed I get this error:
NullReferenceException: Object reference not set to an instance of an object
Soldiers.Soldier.attack (Soldiers.Soldier other, Double mult) (at C:/Users/AdminBenni/documents/visual studio 2017/Projects/Soldiers/Soldiers/Class1.cs:160)
AttackOther.Update () (at Assets/Scripts/AttackOther.cs:19)
This makes me confused because as far as I know everything I'm referencing actually exists.
All help appreciated :)
To solve null reference exceptions, you should find the line that's causing it (line 160 in your class1.cs script, which either has an incorrect filename or you haven't shown it to us), then deter$$anonymous$$e which field you're referencing is null when it shouldn't be, then figure out where, why, and when the field is being set to null. Then, you can either fix what's causing it to be null, or go back to the line that throws the error and handle what should happen when that field is null.
As for deter$$anonymous$$ing which field is null:
as far as I know everything I'm referencing actually exists
If everything you're referencing actually existed, you wouldn't get a null reference error, so "As far as I know" isn't enough to help you here. Use things like this to figure out what's null when it shouldn't be:
if (somefield == null)
{
Debug.Log("Somefield is null!");
}