0

I'm trying to modify another script called ResourceBank from a script called ResourceManipulation.

I Try to use namespace to reference ResourceBank but it causes nullReferenceException when i use unity UI (TMP) button to activate "public void ManipulateResources()" part.

Debugger says that line "ResourceBank.instance.moneyAmount += moneyToBeGained;" triggers the NRE;

Any idea how to not cause nullreferenceException when activating "public void ManipulateResources()"?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using pyrryr.caps.resourcebank;

namespace pyrryr.caps.resourcemanipulation
{
    public class ResourceManipulation : MonoBehaviour
    {
        public ResourceManipulation instance;
        private void Awake()
        {
            instance = this;
        }

        private ResourceBank ResourceBank;
        
        public int moneyToBeGained;
        public int moneyToBeLost;
        [Space(10)]
        public int suppliesToBeGained;
        public int suppliesToBeLost;
        [Space(10)]
        public int securitysuppliesToBeGained;
        public int securitysuppliesToBeLost;
        [Space(10)]
        public int sciencesuppliesToBeGained;
        public int sciencesuppliesToBeLost;

        

        public void ManipulateResources()
        {
            ResourceBank.instance.moneyAmount += moneyToBeGained;
            ResourceBank.instance.moneyAmount -= moneyToBeLost;
            ResourceBank.instance.suppliesAmount += suppliesToBeGained;
            ResourceBank.instance.suppliesAmount -= suppliesToBeLost;
            ResourceBank.instance.securitySuppliesAmount += securitysuppliesToBeGained;
            ResourceBank.instance.securitySuppliesAmount -= securitysuppliesToBeLost;
            ResourceBank.instance.researchSuppliesAmount += sciencesuppliesToBeGained;
            ResourceBank.instance.researchSuppliesAmount -= sciencesuppliesToBeLost;
            ResourceBank.instance.UpdateResources();
        }       
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
using UnityEngine.UI;
using pyrryr.caps.resourcemanipulation;

namespace pyrryr.caps.resourcebank
{
    public class ResourceBank : MonoBehaviour
    {
        public ResourceBank instance;
        private void Awake()
        {
            instance = this;
        }

        public int moneyAmount;
        public TMP_Text moneyAmountText;

        public int suppliesAmount;
        public TMP_Text suppliesAmountText;

        public int securitySuppliesAmount;
        public TMP_Text securitySuppliesAmountText;

        public int researchSuppliesAmount;
        public TMP_Text researchSuppliesAmountText;

        //Resource Modifiers

        

        public void UpdateResources()
        {
            moneyAmountText.text = moneyAmount.ToString();
            suppliesAmountText.text = suppliesAmount.ToString();
            securitySuppliesAmountText.text = securitySuppliesAmount.ToString();
            researchSuppliesAmountText.text = researchSuppliesAmount.ToString();
        }
    }

}
derHugo
  • 83,094
  • 9
  • 75
  • 115
pyrryr
  • 1
  • Please use the correct tags! `unityscript` is or better was a JavaScript flavor like custom language used in early Unity versions and is long deprecated by now! Your script is clearly `c#`! – derHugo May 21 '21 at 17:58

1 Answers1

0

NullReferenceException means one of the objects you reference isn't set to anything. So in your ManipulateResources function, either ResourceBank is null, or ResourceBank.instance is null (you can see which one by setting a breakpoint and hovering over).

Whatever is supposed to set your ResourceBank member doesn't do its job, or Awake() is never called.

Wam
  • 810
  • 1
  • 8
  • 19