Returning a random string

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!

1 Like
   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).
2 Likes

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

Privacy & Terms