Nullreferenceexception: Object Reference not set to an instance of an Object (Unity)

Asked

Viewed 601 times

0

I saved my project, I closed the Unity and after I reopened the project, to my surprise, an error was presented on the console:

Nullreferenceexception: Object Reference not set to an instance of an Object Uimanager.Setuseable (Actionbutton btn, Iuseable useable) (at C:/Users/Mystic Rabbit/Desktop/Gamemaker/RPG V9.1/RPG/Assets/Scripts/Managers/Uimanager.Cs:111) Uimanager.Start () (at C:/Users/Mystic Rabbit/Desktop/Gamemaker/RPG V9.1/RPG/Assets/Scripts/Managers/Uimanager.Cs:53)

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIManager : MonoBehaviour
{
    private static UIManager instance;

    public static UIManager MyInstance
    {
        get
        {
            if (instance == null)
            {
                instance = FindObjectOfType<UIManager>();
            }

            return instance;
        }
    }

    /// <summary>
    /// A reference to all the action buttos
    /// </summary>
    [SerializeField]
    private ActionButton[] actionButtons;

    [SerializeField]
    private GameObject targetFrame;

    private Stat healthStat;

    [SerializeField]
    private Image portraitFrame;

    [SerializeField]
    private CanvasGroup keybindMenu;

    private GameObject[] keybindButtons;

    private void Awake()
    {
        keybindButtons = GameObject.FindGameObjectsWithTag("Keybind");
    }

    // Use this for initialization
    void Start()
    {
        healthStat = targetFrame.GetComponentInChildren<Stat>();

        SetUseable(actionButtons[0], SpellBook.MyInstance.GetSpell("Fireball"));
        SetUseable(actionButtons[1], SpellBook.MyInstance.GetSpell("Frostbolt"));
        SetUseable(actionButtons[2], SpellBook.MyInstance.GetSpell("Thunderbolt"));
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Escape))
        {
            OpenCloseMenu();
        }

    }

    public void ShowTargetFrame(NPC target)
    {
        targetFrame.SetActive(true);

        healthStat.initialized(target.MyHealth.MyCurrentValue, target.MyHealth.MyMaxValue);

        portraitFrame.sprite = target.MyPortrait;

        target.healthChanged += new HealthChanged(UpdateTargetFrame);

        target.characterRemoved += new CharacterRemoved(HideTargetFrame);
    }

    public void HideTargetFrame()
    {
        targetFrame.SetActive(false);
    }

    public void UpdateTargetFrame(float health)
    {
        healthStat.MyCurrentValue = health;
    }

    public void OpenCloseMenu()
    {
        keybindMenu.alpha = keybindMenu.alpha > 0 ? 0 : 1;
        keybindMenu.blocksRaycasts = keybindMenu.blocksRaycasts == true ? false : true;
        Time.timeScale = Time.timeScale > 0 ? 0 : 1;
    }

    public void UpdateKeyText(string key, KeyCode code)
    {
        Text tmp = Array.Find(keybindButtons, x => x.name == key).GetComponentInChildren<Text>();
        tmp.text = code.ToString();
    }

    public void ClickActionButton(string buttonName)
    {
        Array.Find(actionButtons, x => x.gameObject.name == buttonName).MyButton.onClick.Invoke();
    }

    public void SetUseable(ActionButton btn, IUseable useable)
    {
        btn.MyButton.image.sprite = useable.MyIcon;
        btn.MyButton.image.color = Color.white;
        btn.MyUseable = useable;

    }
}

I am leaving the youtube link with the exact tutorial steps that I am having this problem: https://www.youtube.com/watch?v=t5PJdhJ97uo

I recorded a video also doing the final tests with positive results, then restarting the software and getting new negative results. https://youtu.be/kTabDddtjww

Remember, I did the entire tutorial 9.2 up to the last second and got the same positive results as the teacher. Just afterward to restart the software, this error appeared on the console.

If I delete lines 53 to 55 or lines 111 to 113, the error disappears. Of course the spells will not be in the action bar as in the tutorial, but the error disappears. The problem is happening in these lines.

I did the tutorial 3 times to make sure that I had not made any mistake in the process. What I find strange is that everything works perfectly until restart Unity.

1 answer

0

Hi, make sure you have the private ActionButton[] actionButtons; setados in the inspector of Unity3d! because this variable is null.

When class will initialize in method Start there are no elements in the variable ActionButton and you pass them on to the method SetUseable as null also, so the error appears in two places in the code.

  • Thank you for answering! Apparently everything in Unity is configuring correctly: shortener.com.br/vzZ17 Look at this video I posted: https://www.youtube.com/watch?v=kTabDddtjww&feature=youtu.be I can make all the spells work correctly, but after restarting Unity, he loses all references... I did the tutorial 3 times, methodically, step by step and always manage to configure the Skills, but after restarting it loses the references

  • So when you close Unity you lose the elements set in actionButtons that? if yes try to open Unity as administrator or create a Prefab of this UI so that these elements are not rebooted when you open Unity.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.