I know that this has probably been asked many times but I spent 1 hour to find a solution in the web about it but to no avail.
I have a string array with 4 strings inside, then I created a function that says: if something.text equals “Africa” make userCountry.text equal to a randomly picked string from this specific string array, FOR MORE CLARIFICATION check the image below:
Don’t do stuff with floats when indexing (and it should be FloorToInt) when you can use integers.
Here’s an example that uses a nice extension method to get a random item from an array.
using UnityEngine;
public class Example : MonoBehaviour
{
void Start()
{
string[] fruits = { "Apple", "Banana", "Orange" };
print(fruits.RandomItem());
print(fruits.RandomItem());
print(fruits.RandomItem());
print(fruits.RandomItem());
print(fruits.RandomItem());
}
}
public static class ArrayExtensions
{
// This is an extension method. RandomItem() will now exist on all arrays.
public static T RandomItem<T>(this T[] array)
{
return array[Random.Range(0, array.Length)];
}
}
does it work if you use a fixed index instead of your randomizer? Let's say: userCountry.text=countriesAfrica[1];
– ElDo