• 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
Question by MagicStyle · Oct 28, 2017 at 03:57 PM · scripting problemrandomcolorrandom.range

How to generate a random color?

I tried to generate a random color like this:

 Color background = new Color(
     (float)Random.Range(0, 255), 
     (float)Random.Range(0, 255), 
     (float)Random.Range(0, 255)
 );

But I always get white as result.

What am I doing wrong?

Comment
mdrunk
jjmontes
stefanogiovannini87
yektasarioglu
honor0102

People who like this

5 Show 0
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

5 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by Hellium · Oct 28, 2017 at 09:10 PM

As suggested by Dragate, the Color struct needs values between 0 and 1, as indicated in the documentation

Representation of RGBA colors.

This structure is used throughout Unity to pass colors around. Each color component is a floating point value with a range from 0 to 1.


In order to use values between 0 and 255, you need the Color32 class :

  Color32 background = new Color32(
      Random.Range(0, 255), 
      Random.Range(0, 255), 
      Random.Range(0, 255),
      255
  );

 // Color version

  Color background = new Color(
      Random.Range(0f, 1f), 
      Random.Range(0f, 1f), 
      Random.Range(0f, 1f)
  );
Comment
MagicStyle
cvbattum
unity_5DwmbQAYGJt1Ww
SlimeProphet
ZackOfAllTrades
stefanogiovannini87
omursun
yektasarioglu
honor0102
tokar_dev
uniquevables
Ideator

People who like this

10 Show 0 · 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

Answer by RazaTech · Feb 17, 2019 at 05:28 PM

UnityEngine.Random.ColorHSV() generate Random color

Comment
bstockwell
mdrunk
jjmontes
john_unity389
nitaym
ZackOfAllTrades
pavansai432
honor0102
JayadevHaddadi
alexylb
ddark1990
shaneK001

People who like this

12 Show 0 · 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

Answer by AmmarSalim · Oct 22, 2020 at 08:55 AM

This is the simplest: { // Pick a random, saturated and not-too-dark color GetComponent().material.color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f); }

Comment
Righteousice
honor0102
JayadevHaddadi
SlowSeer

People who like this

4 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 DJ-Antony · Jan 11, 2021 at 09:38 AM 0
Share

Sweet AmmarSalim..i just needed to specify the renderer when attached to my gameObject. But yeh your code worked. Big up ! Thanks

obj.GetComponent().material.color = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);

avatar image SlowSeer · Dec 15, 2022 at 03:00 PM 0
Share

This gives some nice looking colors. Feel like it has a bit of a bias towards green but who knows.

avatar image

Answer by Dragate · Oct 28, 2017 at 06:20 PM

  Color background = new Color(
      Random.Range(0f, 1f), 
      Random.Range(0f, 1f), 
      Random.Range(0f, 1f)
  );
Comment
MagicStyle
QQQ_QQQ
unity_5DwmbQAYGJt1Ww

People who like this

3 Show 0 · 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

Answer by krokots · Oct 28, 2017 at 05:55 PM

Maybe try this:

 string seed = Time.time.ToString ();
 System.Random random = new System.Random (seed.GetHashCode ());
 Color background = new Color(
     (float)random.Next(0, 255), 
     (float)random.Next(0, 255), 
     (float)random.Next(0, 255)
 );
 

Comment

People who like this

0 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 MagicStyle · Oct 28, 2017 at 10:49 PM 0
Share

I get only white back, exactly like on my code

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

183 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 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 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 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 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 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

Color Randomize when object spawn help 0 Answers

Different random number - Same script - Different cloned object 0 Answers

Random.Range code problem or problem with Unity? 1 Answer

how to randomly instantiate prefabs but each with it's own probability? 2 Answers

How do I generate a random number in regular time intervals? 0 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