-1

I'm making a memory puzzle in unity, but I get an error when I click on a button. It is supposed to show me an image that is tied to the button, then I click another button. If it is the same thing, the buttons disappear, and if they don't they change back to the default image. The script also is checking weather the game is over or not. The error from is showing me this line: "_firstGuessIndex = int.Parse(UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject.name);" but I don't know weather that's were it's coming from You can also find the tutorial here.

Error:

NullReferenceException: Object reference not set to an instance of an object MemoryPuzzleController.PickButton () (at Assets/Scripts/Memory Puzzle/MemoryPuzzleController.cs:73) MemoryPuzzleController.b__17_0 () (at Assets/Scripts/Memory Puzzle/MemoryPuzzleController.cs:63) UnityEngine.Events.InvokableCall.Invoke () (at <4014a86cbefb4944b2b6c9211c8fd2fc>:0) UnityEngine.Events.UnityEvent.Invoke () (at <4014a86cbefb4944b2b6c9211c8fd2fc>:0) UnityEngine.UI.Button.Press () (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:70) UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/UI/Core/Button.cs:114) UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:57) UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/ExecuteEvents.cs:272) UnityEngine.EventSystems.EventSystem:Update() (at Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:501)

Script:

`

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

public class MemoryPuzzleController : MonoBehaviour
{
    public List<Button> buttons = new List<Button>();
    public List<Sprite> spritePuzzles = new List<Sprite>();
    public Sprite[] puzzles;
    public GameObject door;
    public Sprite startImage;
    public bool _firstGuess, _secondGuess;
    public int _guessCount;
    public int _correctGuesses;
    public int _gameGuesses;
    public int _firstGuessIndex, _secondGuessIndex;
    public string _firstGuessName, _secondGuessName;

    void Start()
    {
        GetButtons();
        AddListeners();
        AddImages();
        RandomShuffle(spritePuzzles);
        _gameGuesses = spritePuzzles.Count / 2;
    }

    void GetButtons()
    {
        GameObject[] objects = GameObject.FindGameObjectsWithTag("PuzzleButton");

        for(int i = 0; i < objects.Length; i++)
        {
            buttons.Add(objects[i].GetComponent<Button>());
            buttons[i].image.sprite = startImage;
        }
    }

    void AddImages()
    {
        int looper = buttons.Count;
        int index = 0;

        for (int i = 0; i < looper; i++)
        {
            if (index == looper / 2)
            {
                index = 0;
            }

            spritePuzzles.Add(puzzles[index]);

            index++;
        }
    }

    void AddListeners()
    {
        foreach (Button button in buttons)
        {
            button.onClick.AddListener(() => PickButton());
        }
    }

    void PickButton()
    {
        if (!_firstGuess)
        {
            _firstGuess = true;

            _firstGuessIndex = int.Parse(UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject.name);
            _firstGuessName = spritePuzzles[_firstGuessIndex].name;
            buttons[_firstGuessIndex].image.sprite = spritePuzzles[_firstGuessIndex];
        }
        else if (!_secondGuess)
        {
            _secondGuess = true;

            _secondGuessIndex = int.Parse(UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject.name);
            _secondGuessName = spritePuzzles[_secondGuessIndex].name;
            buttons[_secondGuessIndex].image.sprite = spritePuzzles[_secondGuessIndex];
            _guessCount++;
            StartCoroutine(PuzzleMatch());
        }
    }

    IEnumerator PuzzleMatch()
    {
        yield return new WaitForSeconds(.5f);

        if (_firstGuessName == _secondGuessName && _firstGuessIndex != _secondGuessIndex)
        {
            yield return new WaitForSeconds(.5f);
            buttons[_firstGuessIndex].interactable = false;
            buttons[_secondGuessIndex].interactable = false;
            buttons[_firstGuessIndex].image.color = new Color(0, 0, 0, 0);
            buttons[_secondGuessIndex].image.color = new Color(0, 0, 0, 0);
            IsGameFinished();
        }
        else
        {
            yield return new WaitForSeconds(.5f);
            buttons[_firstGuessIndex].image.sprite = startImage;
            buttons[_secondGuessIndex].image.sprite = startImage;
        }
        yield return new WaitForSeconds(.5f);

        _firstGuess = _secondGuess = false;
    }

    void IsGameFinished()
    {
        _correctGuesses++;

        if (_correctGuesses == _gameGuesses)
        {
            door.SetActive(false);
        }
    }

    void RandomShuffle(List<Sprite> shuffleList)
    {
        for (int i = 0; i < shuffleList.Count; i++)
        {
            Sprite temp = shuffleList[i];
            int randomIndex = Random.Range(0, shuffleList.Count);
            shuffleList[i] = shuffleList[randomIndex];
            shuffleList[randomIndex] = temp;
        }
    }
}`

Again, it's supposed to show me an image that is tied to the button, then I click another button. If it is the same thing, the buttons disappear, and if they don't they change back to the default image

  • 1
    Duplicate: [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp Mar 19 '23 at 18:06

1 Answers1

0

You have tried to Parse an object that was null (i.e. UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject wasn't allocated in memory by the time of your call).

You need to dig up to UnityEngine.EventSystems.EventSystem.current.currentSelectedGameObject to check why it wasn't allocated. The exception you highlighted gives you idea on where to dig: e.g. MemoryPuzzleController.cs file at line 73and up above the chain.

Please also check this topic: What is a NullReferenceException, and how do I fix it?

Humble Newbie
  • 98
  • 1
  • 11