Getting null reference exception tried many things but nothing worked :(

Getting this big error but i could not find why it is coming
I have attached this script to jet menu controller gameobject and left and right button are assigned proper funtions from jet menu controller gameobject select btn is also assigned proper function from missile select script which is attached to jet menu controller gameobject.
Left and right arrow are working but when clicked give this error. How can i solve this problem?

error of line 70

NullReferenceException: Object reference not set to an instance of an object
MissileSelect.CheckIfMissileIsUnlock () (at Assets/Scripts/MissileSelect.cs:70)
MissileSelect.NextMissile () (at Assets/Scripts/MissileSelect.cs:52)
UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent.cs:166)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent/UnityEvent_0.cs:58)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:66)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:108)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update()

error of line 115

NullReferenceException: Object reference not set to an instance of an object
MissileSelect.SelectMissile () (at Assets/Scripts/MissileSelect.cs:115)
UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent.cs:166)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent/UnityEvent/UnityEvent_0.cs:58)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:66)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:108)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update()

And the code is here ,error at line 70 and 115

 public GameObject[] availableMissiles;
    private int currentMissileIndex;
    public Text selectedText;
    public GameObject CoinIcon;
    public Image selectBtn_Image;
    public Sprite button_Green, button_Blue;
    private int missilePrice;
    private bool[] missiles;
    public Text coinScoreText;
    // Start is called before the first frame update
    private string missile1 = "0", missile2 = "2000", missile3 = "5000", missile4 = "8000";
    void Start()
    {
        InitializeCharacters();
    }

    // Update is called once per frame
    void Update()
    {

    }
    void InitializeCharacters()
    {
        currentMissileIndex = GameManager.instance.missileSelected_Index;
        for (int i = 0; i < availableMissiles.Length; i++)
        {
            availableMissiles[i].SetActive(false);
        }

        availableMissiles[currentMissileIndex].SetActive(true);
        missiles = GameManager.instance.missiles;
    }
    public void NextMissile()
    {
        availableMissiles[currentMissileIndex].SetActive(false);
        if (currentMissileIndex + 1 == availableMissiles.Length)
        {
            currentMissileIndex = 0;
        }
        else
        {
            currentMissileIndex++;
        }
        availableMissiles[currentMissileIndex].SetActive(true);
        CheckIfMissileIsUnlock();
    }
    public void PreviousMissile()
    {
        availableMissiles[currentMissileIndex].SetActive(false);
        if (currentMissileIndex - 1 == -1)
        {
            currentMissileIndex = availableMissiles.Length - 1;
        }
        else
        {
            currentMissileIndex--;
        }
        availableMissiles[currentMissileIndex].SetActive(true);
        CheckIfMissileIsUnlock();
    }
    void CheckIfMissileIsUnlock()
    {
        if (missiles[currentMissileIndex] == true) // error at this line
        {
            //if the jet is unlocked
            CoinIcon.SetActive(false);
            if (currentMissileIndex == GameManager.instance.missileSelected_Index)
            {
                selectBtn_Image.sprite = button_Green;
                selectedText.text = "Selected";
            }
            else
            {
                selectBtn_Image.sprite = button_Blue;
                selectedText.text = "Select?";
            }
        }
        else
        {
            //if the jet is locked
            selectBtn_Image.sprite = button_Blue;
            CoinIcon.SetActive(true);
            if (currentMissileIndex == 0)
            {
                selectedText.text = missile1;
                missilePrice = 0;
            }//change price of jet here
            if (currentMissileIndex == 1)
            {
                selectedText.text = missile2;
                missilePrice = 2000;
            }
            if (currentMissileIndex == 2)
            {
                selectedText.text = missile3;
                missilePrice = 5000;
            }
            if (currentMissileIndex == 3)
            {
                selectedText.text = missile4;
                missilePrice = 8000;
            }

        }
    }
    public void SelectMissile()
    {
        if (!missiles[currentMissileIndex])// error at this line
        {
            //if the jet is not unlocked- meaninig unlock it
            if (currentMissileIndex != GameManager.instance.missileSelected_Index)
            {
                //unlock jet if u have enough coins
                if (GameManager.instance.coinScore >= missilePrice)//change price of jet here
                {
                    GameManager.instance.coinScore -= missilePrice;//change price of jet here
                    selectBtn_Image.sprite = button_Green;
                    selectedText.text = "Selected";
                    CoinIcon.SetActive(false);
                    missiles[currentMissileIndex] = true;
                    coinScoreText.text = GameManager.instance.coinScore.ToString();
                    GameManager.instance.missileSelected_Index = currentMissileIndex;
                    GameManager.instance.missiles = missiles;
                    GameManager.instance.SaveGameData();
                }
                else
                {
                    print("not enough coins");
                }
            }
        }
        else
        {
            selectBtn_Image.sprite = button_Green;
            selectedText.text = "Selected";
            GameManager.instance.missileSelected_Index = currentMissileIndex;
            GameManager.instance.SaveGameData();
        }
    }
}

currentMissileIndex is int type and it is assigned value from gameManager script
missiles is bool type array.
Please help me to solve this problem.

Please take screenshots - maybe with the snipping tool or snip & sketch - rather than an actual picture for better image clarity.

If you don’t mind you could zip your project and attach/link it.

Hi Divasray,

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

Hope this helps :slight_smile:


See also;

1 Like

sorry for Inconvenience, now i have edited that please see to it.

sorry for inconvenience, now i have edited that please see to it.

Thank you. That’s much better. :slight_smile:

In which course and lecture are you?

The null reference means the object you’re accessing hasn’t been created.
missiles = GameManager.instance.missiles

Put an if (missiles == null) {Debug.LogError(“Missiles is null”);}
after you’ve assigned missiles.

It’s possible that missiles in your GameManager.instance hasn’t been initialised yet when this code runs in your Start method in the missiles.cs file.

I am trying to level up laser defender by making store for player to choose missiles and lasers.

Yes you are right. I have implemented if statement and if statement is running. What i should do now :frowning:

You need to use Awake and Start. For reference:

https://docs.unity3d.com/ScriptReference/MonoBehaviour.Awake.html?_ga=2.207593296.279825549.1580544139-461143041.1566564133

https://learn.unity.com/tutorial/awake-and-start

And essentially:

Blockquote Awake is for initializing my stuff, Start is for initializing your stuff

from https://forum.unity.com/threads/the-proper-use-of-awake-and-start.454314/

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms