Returning a random string

cu2_s02_introducing_random_behaviour

#1

Hello, I wanted to create a random string to be returned for a wrong answer under “Debug.LogError”. It seems “Random.Range” will only return numbers. What would be another way to return a random string for wrong answers. I couldn’t find anything online that was relevant. Thanks!


#2
   string[] lossStrings = { "string one. ", "string two. ", "string three. " };

   string RandomLossString()
   {
      return lossStrings[Random.Range(0,lossStrings.Length)];
   }

   Debug.Log(RandomLossString());

To explain what this code is about:

  1. The variable lossStrings is a “string array”. An array is a group of similar data, in this case: strings.
  2. The function RandomLossString returns one member of the string array at random.
  3. If no other part of the class needs access to the array, you could move it inside the function (make it local).

#3

Awesome! Thank you very much for the great info Jack.