0

Hello guys i'm trying to write a game scenario which includes multiple levels.In the scenario the user will try to memorize the words on the screen then try to choose right ones by clicking buttons. I created multiple buttons as a GameObject (for specific reasons)

so here's how it goes

    public GameObject[] buttons = new GameObject[7];

These are my buttons.I created them in my canvas as buttons but in script i treat them like a GameObject.

 void wordsToButton()
{        
    for(int i = 0; i < wordsTxt.Length; i++)
    {          
        buttons[i].GetComponentInChildren<Text>().text = wordsTxt[i].GetComponent<Text>().text;
        buttons[i].transform.position = new Vector2(Random.Range(30, Screen.width - 20), Random.Range(30, Screen.height - 50));
    }
    for(int i=0; i <differentWords.Length; i++)
    {
        buttons[i+5].GetComponentInChildren<Text>().text = differentWords[i].GetComponent<Text>().text;
        buttons[i+5].transform.position = new Vector2(Random.Range(30, Screen.width - 20), Random.Range(30, Screen.height - 50));
    }
}

In here i am trying to edit button's text and give it a random position.Additionaly i also put some wrong words.It works fine.

 public void buttonListeners()
{
    for(int i=0; i < buttons.Length-1; i++)
    {
        buttons[i].GetComponent<Button>().onClick.AddListener(() =>
        {
            int n = i;
            if(buttons[n].GetComponent<Text>().text == differentWords[0].GetComponent<Text>().text || buttons[n].GetComponent<Text>().text == differentWords[1].GetComponent<Text>().text)
            {
                correctAnswers--;
            }
            else
            {
                correctAnswers++;
            }
        });
    }
}

In here i gave listeners to buttons.When user click on buttons it checks for correct answer.

 public void level2()
{
    if (levels[2].activeSelf == true)
    {
        differentWords[0].SetActive(false);
        differentWords[1].SetActive(false);
        levels[0].SetActive(false);
        levels[1].SetActive(false);
        levels[3].SetActive(false);
        timer1 = 10;
        InvokeRepeating("countDown1", 0.01f , 1);           
    }
    wordsToButton();
    buttonListeners();
}

This is the level which i talked about.So when i click a button on the screen it gives me this error:

NullReferenceException: Object reference not set to an instance of an object Word+c__AnonStorey0.<>m__0 () (at Assets/categories/Word.cs:184)

line 184 is :

 if(buttons[n].GetComponent<Text>().text == differentWords[0].GetComponent<Text>().text || buttons[n].GetComponent<Text>().text == differentWords[1].GetComponent<Text>().text)

What should i do ? I know that I'm trying to reach null values.But it is not null actually, thus far my code works perfectly fine and i can see button's texts, all of them correct but when i click a button,it gives me this error.

Edit: I understood why it gives me errors.Writing this for helping some people. Because i try to get a Button's Text component. Which is null. I fixed my code with

if(buttons[n].GetComponentInChildren<Text>().text == differentWords[0].GetComponent<Text>().text || buttons[n].GetComponentInChildren<Text>().text == differentWords[1].GetComponent<Text>().text)
  • I might be wrong but consider moving your `int n = i;` inside a method called `buttonListeners` outside of the handler method. – mrogal.ski Nov 13 '17 at 13:52
  • I tried it but it is still giving me same error. – xBaskerville Nov 13 '17 at 13:53
  • 1
    This is mostly a case for debugger so I doubt that anyone can tell you what is wrong in here. I can guess that this can be caused by either no `Text` component on specified `GameObject` or a `null` value coming out of the array. – mrogal.ski Nov 13 '17 at 13:56
  • 1
    put a breakpoint on line 184, debug the application, go to unity, start your scene and look what is in the `differentWords` and `buttons` arrays when the breakpoint is hit. – Timothy Groote Nov 13 '17 at 13:58
  • Yeah I get that. But i can see texts on that `GameObject` s so doesn't that mean they are not `null`? Thanks for the answers by the way. – xBaskerville Nov 13 '17 at 13:59
  • that means the `differentWords` variables were not null, but the `buttons` variable at that specific index might be. i have my suspicion that the code generating the buttons (`wordsToButton()`) does not exactly do what you wanted it to. (i also suspect your `buttons[i+5]` portion) – Timothy Groote Nov 13 '17 at 14:01
  • @TimothyGroote thanks for the reply. I'll try that. I'm guessing that it'll show me `null` but i can't understand why exactly? – xBaskerville Nov 13 '17 at 14:01
  • @xBaskerville you can see what is inside every index of the array at that point. my guess is that you'll see a `null` in the `buttons` array somewhere around index 4 – Timothy Groote Nov 13 '17 at 14:01
  • @TimothyGroote all of them `null` so i'm gonna rebuild my listeners i guess,thanks for the replies again. – xBaskerville Nov 13 '17 at 14:33

0 Answers0