Pick a random string from a string array C#
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 :)
does it work if you use a fixed index ins$$anonymous$$d of your randomizer? Let's say:
userCountry.text=countriesAfrica[1];
It does work only if I remove the IF statement in line 34... Apparently there is also something wrong with the if statement. :(
Ah I see. you made your string Arrays local in your Start() function, therefor you can't Access them in your userCountryShow() function. $$anonymous$$ove them out of functions
Answer by Statement · Oct 25, 2015 at 04:45 PM
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)];
}
}
I change T and T[] for countriesAfrica(my string array name), however it says 'UIYourNameInputField.ArrayExtensions.RandomItem(this countriesAfrica[])': Extensions methods cannot be defined in a nested class.
Is it that hard to choose a random string from a SPECIFIC array based on the text value?
What I am trying to do is the following: -> Choosing a continent from a dropdown menu. Let's say we pick Africa; -> When Africa is chosen from the dropdown menu, another text object takes this value and displays it (you showed me this 2 days ago); -> Based on what continent is displayed in this text object, we pick a random string from array called countriesAfrica and we display it on a second text object; -> If we choose Africa on the dropdown menu (displays the option chosen on the first text object), we pick a random string from countriesAfrica array, if we pick Europe, we pick a random string from countriesEurope array and so on.
:(
I change T and T[] for countriesAfrica
... That doesn't make sense.
All you need to do is call RandomItem() on the array you want to use. For example, either of these lines...
userCountry.text = countriesAfrica.RandomItem();
userCountry.text = countriesAsia.RandomItem();
userCountry.text = countriesEurope.RandomItem();
userCountry.text = countriesAfrica.RandomItem();
userCountry.text = countriesNorthAmerica.RandomItem();
userCountry.text = countriesSouthAmerica.RandomItem();
Extensions methods cannot be defined in a nested class.
This means you can't put the class ArrayExtensions inside your Script. Place it after, or move it into a file of its own. Look at my example. ArrayExtensions class comes AFTER the Example class. Not inside it.
Thank you for your time but I managed to do the random thing by my own:
However, when I test it, the userCountry shows not the current text change but the previous one, as can be seen below: ! ! ! !