Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by AngryCSharpNerd · Sep 26, 2012 at 09:12 PM · c#guiarrayminecraftsplash

C# Random String Array help

 void OnGUI(){
     string[] splashes = {"Less than one sold!", "lol","lmao","what?"};
     System.Random random = new System.Random();
     string quote = splashes[random(splashes)];
     GUI.Label(new Rect(50,500,300,100),quote);
 }

//I'm trying to create a GUI which will have random sentences in the splashes string. I have no idea how to do this, but can someone please find a way on how to do this? So for example, when I start up the game, it will state "Less than one sold!", "lol", "lmao", or "what?". If you have every played Minecraft where there are those hundreds of different yellow sentence splashes at an angle, then you should know I want.

Comment
Add comment · Show 1
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 hvilela · Sep 26, 2012 at 09:30 PM 0
Share

What is the problem with the code you have posted? Looks like you're doing it already.

2 Replies

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

Answer by Eric5h5 · Sep 26, 2012 at 10:57 PM

To make things easier, use Random.Range (look it up in the Unity docs), and you should only do it once, in Start or whatever, not every frame in OnGUI, otherwise it will constantly flicker between random strings.

Comment
Add comment · Show 2 · 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 Jesdisciple · Sep 26, 2012 at 11:06 PM 0
Share

facepalm I didn't notice the OnGUI part - especially nasty with the `new`. But what's the point of using Random.Range? I suppose Unity uses its own internal System.Random singleton that way, so you won't risk running into the issues associated with multiple Random instances, but are there any other benefits?

avatar image Eric5h5 · Sep 26, 2012 at 11:19 PM 0
Share

As I mentioned, it's easier.

 string quote = splashes[Random.Range(0, splashes.Length)];

Unity doesn't use System.Random for that, by the way; they use their own routine. In some cases where you need more control it would be appropriate to use System.Random, but for typical general randomness, UnityEngine.Random is somewhat simpler to work with.

avatar image
1

Answer by Jesdisciple · Sep 26, 2012 at 10:18 PM

You almost had it. See here: http://msdn.microsoft.com/en-us/library/zd1bc8e5.aspx

That method returns an integer from 0 up to but not including `maxValue`, perfect for use as an index for an array of size `maxValue`. Your modified code would be:

 string quote = splashes[random.Next(splashes.Length)];

I should note that I didn't run this code; I'll gladly correct it if necessary.

Comment
Add comment · 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

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

11 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

Related Questions

holding GUITextures in a array? and changing the size from script? 2 Answers

Multiple Cars not working 1 Answer

GUISttyle NullReferenceException error. what am I missing ? 1 Answer

How to select an Game Object from an Item List 0 Answers

One variable set TRUE when another must be false. TOGGLE 3 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