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:
- The variable lossStrings is a “string array”. An array is a group of similar data, in this case: strings.
- The function RandomLossString returns one member of the string array at random.
- 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.