Hello,

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:

However, the assembly tells me that userCountry[…] is not valid expression in this context. So my question is how to link it to this specific array!

→ I know how to make it random

Any help will be appreciated :slight_smile:

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)];
    }
}