UnityNinja Community
Would you like to react to this message? Create an account in a few clicks or log in to continue.

UnityNinja CommunityLog in

UnityNinja - Video Game Development Community, including Resources, Forums, Marketplace & More.


descriptionReferring integers between scripts in C# - NullReferenceException: Object reference not set to an instance of an object EmptyReferring integers between scripts in C# - NullReferenceException: Object reference not set to an instance of an object

more_horiz
Hello, I'm a bit new in C# and Unity and I've come across a problem with referring integers between two files. My code looks basically like (only those parts that are connected with my problem):

PlayerController.cs :

   public Button  AtakujBtn;
    public int StartHP = 100, StartDMG = 10, MyHP = 100,
    ZombieScript zombieScript;
    void Start()
    {
        zombieScript = this.GetComponent<ZombieScript>();
        Button Atakuj_button = AtakujBtn.GetComponent<Button>();
        Atakuj_button.onClick.AddListener(AtakujZmb);
    }
    public void AtakujZmb()
    {
        zombieScript.ZombieAttack(ref StartDMG, ref StartHP, ref MyHP);
    }

and ZombieScript.cs:

    public GameObject InfoZmb, Zombie, ZombieAlive, ZombieDead;
    public int ZombieHP = 100, ZombieBasicHP = 100;
    public Text hpText, zombieHPText;
   
    public void ZombieAttack(ref int StartDMG, ref int StartHP, ref int MyHP)
    {
        int PlayerATK = Random.Range(StartDMG * 2, StartDMG * 4);
        ZombieHP -= PlayerATK;
        zombieHPText.text = "Zombie HP: " + ZombieHP + "/" + ZombieBasicHP;
        if (ZombieHP > 0)
        {
            int ZombieATK = Random.Range(10, 30);
            MyHP -= ZombieATK;
            hpText.text = "HP: " + MyHP + " / " + StartHP;
        }
        else if (ZombieHP <= 0)
        {
            ZombieAlive.SetActive(false);
            ZombieDead.SetActive(true);
            ZombieHP = ZombieBasicHP;
        }
    }


The error name in Console is:

NullReferenceException: Object reference not set to an instance of an object PlayerController.AtakujZmb () (at Assets/PlayerController.cs:86) ...

The 86 line is the one from PlayerController.cs -> zombieScript.ZombieAttack(ref StartDMG, ref StartHP, ref MyHP);

I know that the code is not perfect (really far from perfect) but I've just started learning and i want to learn how to pass values like those between files, thanks for your help!

descriptionReferring integers between scripts in C# - NullReferenceException: Object reference not set to an instance of an object EmptyRe: Referring integers between scripts in C# - NullReferenceException: Object reference not set to an instance of an object

more_horiz
Did you go into the console and copy the whole entire error?

descriptionReferring integers between scripts in C# - NullReferenceException: Object reference not set to an instance of an object EmptyRe: Referring integers between scripts in C# - NullReferenceException: Object reference not set to an instance of an object

more_horiz
Noo, the whole error is much longer, here it is:

NullReferenceException: Object reference not set to an instance of an object
PlayerController.AtakujZmb () (at Assets/PlayerController.cs:86)
UnityEngine.Events.InvokableCall.Invoke (System.Object[] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:154)
UnityEngine.Events.InvokableCallList.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:637)
UnityEngine.Events.UnityEventBase.Invoke (System.Object[] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:773)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:52)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update()

descriptionReferring integers between scripts in C# - NullReferenceException: Object reference not set to an instance of an object EmptyRe: Referring integers between scripts in C# - NullReferenceException: Object reference not set to an instance of an object

more_horiz
Do you mind posting the entire script?

descriptionReferring integers between scripts in C# - NullReferenceException: Object reference not set to an instance of an object EmptyRe: Referring integers between scripts in C# - NullReferenceException: Object reference not set to an instance of an object

more_horiz
I'm guessing you forgot to assign a variable in the Inspector.

descriptionReferring integers between scripts in C# - NullReferenceException: Object reference not set to an instance of an object EmptyRe: Referring integers between scripts in C# - NullReferenceException: Object reference not set to an instance of an object

more_horiz
privacy_tip Permissions in this forum:
You cannot reply to topics in this forum
power_settings_newLogin to reply