Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Caruzo · Oct 25, 2015 at 04:08 PM · arrayrandomstring

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: alt text

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 :)

xa.png (36.8 kB)
Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ElDo · Oct 25, 2015 at 08:29 PM 0
Share

does it work if you use a fixed index ins$$anonymous$$d of your randomizer? Let's say:

userCountry.text=countriesAfrica[1];

avatar image Caruzo ElDo · Oct 25, 2015 at 08:32 PM 0
Share

It does work only if I remove the IF statement in line 34... Apparently there is also something wrong with the if statement. :(

avatar image ElDo Caruzo · Oct 25, 2015 at 08:38 PM 0
Share

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

Show more comments
Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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)];
     }
 }
Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Caruzo · Oct 25, 2015 at 08:24 PM 0
Share

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.

:(

avatar image Statement Caruzo · Oct 25, 2015 at 09:11 PM 0
Share

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.

avatar image Caruzo Statement · Oct 25, 2015 at 10:23 PM 1
Share

Thank you for your time but I managed to do the random thing by my own: alt text alt text

However, when I test it, the userCountry shows not the current text change but the previous one, as can be seen below: ! ! ! !

xxx1.png (14.2 kB)
xxx2.png (18.6 kB)
Show more comments
Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

34 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to make a random string from array in C#? 1 Answer

Printing a random value from an array. 1 Answer

Random Generator Instantiate Tiles C# = PLEASE HELP = 0 Answers

can't save values from another class to a list 1 Answer

C# If string is equal to any in array 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges