• 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 BraveVN · Apr 08, 2014 at 04:57 PM · colorguitextrandomly

How to change GUIText color randomly ?

In my game I place GUIText many times on screen and I want its color is changed randomly. Example:


  • first time appear: Red.

  • second time appear: Green.

  • Third time appear: Blue.


I try to use this solution: GUIText.material.color = new Color (Random.Range(0, 255), Random.Range(0, 255), Random.Range(0, 255)) but it not work. How can I do this ? Thanks for your help.

Comment
Add comment · Show 2
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 perchik · Apr 08, 2014 at 05:01 PM 0
Share

what do you mean that it didn't work? Did it not do what you expected or did it throw errors?

avatar image BraveVN · Apr 08, 2014 at 05:05 PM 0
Share

the code is not return the random color, It's just show the default color of GUIText in the inspector

1 Reply

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

Answer by perchik · Apr 08, 2014 at 05:16 PM

Well two things:

  1. Color() takes three floats between 0-1 not 0-255. So you should probably do Random.Range(0, 255)/255) for each color.

  2. That's going to work, but I imagine that most colors it generates are brownish. I'd probably create an array of colors of size 16 or so, and manually assign each color to the array. Then generate a random index into that array, so that you get colors that are useful:

    Color[] colors = {Color.red, Color.cyan, Color.yellow }; //etc

    int indx= Random.Range(0,colors.Length);

    GUIText.material.color = colors[indx];

Comment
Add comment · Show 1 · 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 BraveVN · Apr 08, 2014 at 05:28 PM 0
Share

thanks, it's worked for me. Thank you

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

23 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

Related Questions

Animating GUIText material properties 0 Answers

Changing two different objects renderer colour 1 Answer

Shrink and change color of GUI 0 Answers

Changing GUI Text colour when health is low 1 Answer

Change MANUALLY GUIText's.text COLOR 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges