How return the next index from a static word list

Hi I’m trying to return in order the next index from the: private static string[ ] wordListGrandparent. I’m using brackeys tutorial on making a typing game and getting random indexes work fine, but i’m having trouble returning them in order in the public static string GetGrandparentWord() function.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WordGenerator : MonoBehaviour

   
{

   
    private static string[] wordList = { "box", "fax", "cat", "dog", "gut", "put", "sub", "sin",
                                         "max", "sax", "mop", "pet",  "able", "make", "ache", "bake", "acid",
                                         "weed", "acre", "mom", "sidewalk", "robin", "three", "four",  "protect", "periodic", "somber",
                                         "majestic", "jump", "pretty", " wound", "jazzy", "memory", "join", "crack", "grade", "archie",
                                    "kimo", "boot",  "cloudy", "sick",  "mug",  "hot",  "tart",  "dangerous", "rustic", "economic",
                                    "weird",  "cut", "parallel", "wood",   "interrupt",
                                    "guide", "long", "chief", "mom", "signal", "rely", "abortive",
                                    "hair",  "earth", "grate", "proud", "feel",
                                    "hilarious", "addition", "silent", "play", "floor", "numerous",
                                    "friend", "pizzas", "building", "organic", "past", "mute", "unusual",
                                    "mellow", "analyse", "crate", "homely", "protest",
                                    "society", "head", "female", "eager", "heap", "dramatic", "present",
                                    "sin", "box", "pies", "awesome", "root", "available", "sleet", "wax",
                                    "boring", "smash", "anger", "tasty", "spare", "tray", "daffy", "scarce",
                                    "account", "spot", "thought", "distinct", "nimble", "practise", "cream",
                                    "ablaze", "thoughtless", "love", "verdict", "giant",     };

    private static string[] wordListGrandparent = { "dom", "plus", "abby", "equals", "you", "are", "going", "to", "be", "a", "grandparent", };

    public static string GetRandomWord()
    {
        int randomIndex = Random.Range(0, wordList.Length);
        string randomWord = wordList[randomIndex];

        return randomWord;
    }

    public static string GetGrandparentWord()
    {
       
        int nextIndex = 0;
       
        string nextWord = wordListGrandparent[nextIndex];

        return nextWord;
    }
}

Make line 41 a static variable so it isn’t zeroed at every call to GetGPWord()

Then either pre- or post-increment it and return that indexed word.

Be sure to check the size of the grandparent array or else you will get this error, obviously:

Here are some notes on IndexOutOfRangeException and ArgumentOutOfRangeException:

http://plbm.com/?p=236

Steps to success:

  • find which collection it is (critical first step!)
  • find out why it has fewer items than you expect
  • fix whatever logic is making the indexing value exceed the collection
  • remember you might have more than one instance of this script in your scene/prefab

very easy solution, thank you so much!

1 Like