#Object reference exception. Is it from my use of Events, or something else?

1 messages · Page 1 of 1 (latest)

fossil kraken
#

To start with, I'm getting this error:

NullReferenceException: Object reference not set to an instance of an object
AbilityUIManager.PopulateNPCScores (System.Int32 npc_agVal, System.Int32 npc_endVal, System.Int32 npc_chaVal, System.Int32 npc_miVal, System.Boolean isPlayer) (at Assets/Scripts/AbilityUIManager.cs:51)

Which is referring to this line:

            npc_agValDisp.text = npc_agVal.ToString();

Of this code:

public class AbilityUIManager : MonoBehaviour
{
    [Header("NPC Ability Score Value Fields")]
    [SerializeField] TextMeshProUGUI npc_agValDisp;
    [SerializeField] TextMeshProUGUI npc_endValDisp;
    [SerializeField] TextMeshProUGUI npc_chaValDisp;
    [SerializeField] TextMeshProUGUI npc_miValDisp;

    void OnEnable()
        AbilityCheckManager.PopulatedNPCScores += PopulateNPCScores;
    }

    void PopulateNPCScores(int npc_agVal, int npc_endVal, int npc_chaVal, int npc_miVal, bool isPlayer)
    {
        if (!isPlayer)
        {
            npc_agValDisp.text = npc_agVal.ToString();
            npc_endValDisp.text = npc_endVal.ToString();
            npc_chaValDisp.text = npc_chaVal.ToString();
            npc_miValDisp.text = npc_miVal.ToString();
        }
    }

    void OnDisable()
    {
        AbilityCheckManager.PopulatedNPCScores -= PopulateNPCScores;
    }
}

I'm confused, though, because npc_agValDisp.text is correctly working on the First set of integers to go in, but not the second set. I'm wondering if this has to do with PopulateNPCScores somehjow unsubscribing from the event that populates it?

kind breach
#

Work backwards from there

fossil kraken
#

Will do, thanks

#

Could it be that npc_agVal is null, thus that makes npc_agValDisp null, or more specifically it's that npc_agValDisp, by itself, is going null somehow?

kind breach
#

npc_agVal cannot be null

#

it's an int which is a struct/value type

#

npc_agValDisp is null

#

Could be that you have another copy of the script you forgot about

#

or you somehow set it to null