Array index is out of range C#

I’ve made a game of hangman, but it says IndexOutOfRangeException: Array index is out of range.

The error appears in here:
MainGameScript.ChooseWord()
MaingameScript.RestartGame()
MainGameScript.Start()

The problem is probably in this piece of script:

void RestartGame()
	{
		winorlose.enabled = false;
		gameLost = false;
		gameWinner = 0;
		wrongAnswer = 0;

		for (int i = 0; i < hangmanWords.Length; i++) {
			hangmanWords *.GetComponent<Text> ().text = "_";*

_ hangmanWords .SetActive (false);_
* }*

* for (int i = 0; i < hangman.Length; i++) {*
_ hangman .SetActive (false);
* }*_

* ChooseWord ();*
* }*
Could you please help me with this? If this isn’t the right piece of script, please let me know.

The problem does not seem to be here.

You don’t have to guess. Unity will tell you precisely which line of code is the problem, and open Visual Studio precisely on that line for you to fix it.

My guess is that hangmanWords and/or hangman arrays were empty.
When you do a for (int i = 0 ; i < array.Length ; i++) type of loop, if the array is empty, it’ll still run the first cycle and try to access the first entry from the array or list.
You may want to check if (array.Length != 0) before going in the loop, or use a foreach loop instead. Like :

foreach (Gameobject g in hangman)
{
    g.SetActive (false);
}