Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink

You are not connected. Please login or register

Unity Ninja Unity 3D Forum » HELP » Help (General) » Referring integers between scripts in C# - NullReferenceException: Object reference not set to an instance of an object

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

View previous topic View next topic Go down  Message [Page 1 of 1]

RetroSuperHeroo


Member [Lv. 1]
Member [Lv. 1]
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!

View user profile

DeveloperLuke

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

View user profile

RetroSuperHeroo


Member [Lv. 1]
Member [Lv. 1]
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()

View user profile

DeveloperLuke

avatar
Moderator
Moderator
Do you mind posting the entire script?

View user profile

DeveloperLuke

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

View user profile

Sponsored content


View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum