Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Having Trouble With Buttons: Index Out of Range Exception

Discussion in 'Scripting' started by unity_1j7X4B9xCQ0klw, Jun 30, 2020.

  1. unity_1j7X4B9xCQ0klw

    unity_1j7X4B9xCQ0klw

    Joined:
    Jun 30, 2020
    Posts:
    14
    Hello ya'll, I'm a novice Unity user and I've run into a problem whilst trying to make a simple tic tac toe project. Maybe someone can help me.

    I have a grid object with 9 child buttons, the script is supposed to put a sprite on the button when it's pressed, set that button to be non-interactable, and change my 'whoturn' variable to the other player's turn.

    When I push one of the buttons I'm getting an IndexOutOfRangeException in the console. It seems to be a problem with the WhichNumber code in my script. I've attached a couple screen shots of my settings for these buttons; I have those buttons assigned to my game controller object in in the inspector, and I set the On Click options correctly (I think) on each button. The screen shot shows button 0, but the other ones go from 1-8 depending on their location on the grid, if that makes sense.

    I'm sure this is an extremely simple problem, but I'm a beginner and I'm at a loss. Any advice?

    Thanks in advance!
     

    Attached Files:

  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,741
    You have two GameController scripts on your object. Remove one of them, probably the one where the "tictactoe spaces" array is empty.
     
    unity_1j7X4B9xCQ0klw likes this.
  3. unity_1j7X4B9xCQ0klw

    unity_1j7X4B9xCQ0klw

    Joined:
    Jun 30, 2020
    Posts:
    14
    Yep, I definitely didn't notice I had done that. Problem solved, thank you!