Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 evilmonster18 · Jun 30, 2015 at 04:31 PM · c#spritecolorspriterendererrandomize

Problem with a color randomization

So I'm making a 2d game for android where the user is given a circle with a certain color and then, in the middle of other circles with random colors, he has to guess which one has the same color as the one that was given to him.

My problem is that sometimes the random colors from the other circles are very similar to the color of the circle that was given to the user and that may create some confusion to the user. I wanna make the color from the other circles vary taking into account the color of the circle that was given to the user. I already tried to use if's to see if the colors are very similar to randomize them again but that just makes unity crash. If anyone could help me out, that would be great :)

Btw I'm using the sprite renderer from the circles to randomize the color.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by DiegoSLTS · Jun 30, 2015 at 04:58 PM

What's the code making Unity crash? Unity crashes while playing the game are almost everytime caused by infinite loops, so you probably wrote a loop to create another random color and forgot something to break the loop.

To check if one color is similar to another you could convert them to Vector3s (using the rgb values) or treat them as Vector4s, and then using the Vector3.Distance or Vector4.Distance methods to check against a threeshold.

It should be something like:

 while (Vector4.Distance(randomColor,circleColor) < threeshold) {
     randomColor = new Color(Random.Range(0.0,1.0),Random.Range(0.0,1.0),Random.Range(0.0,1.0));
 }
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 evilmonster18 · Jun 30, 2015 at 05:58 PM 0
Share

Thanks for the answer, it worked for me! I was using a lot of if's to check each color paramenter (rgb) individually, when I could use a while loop to check for the entire color xd

avatar image
0

Answer by Dave-Carlile · Jun 30, 2015 at 05:00 PM

From this Stack overflow question...

  public bool AreColorsSimilar(Color c1, Color c2, int tolerance)
  {
      return Math.Abs(c1.R - c2.R) < tolerance &&
             Math.Abs(c1.G - c2.G) < tolerance &&
             Math.Abs(c1.B - c2.B) < tolerance;
  }

As the answer states, if this doesn't work well enough for you you might need to convert to the HSV color space and do a similar comparison there on the hue, saturation, and brightness.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Finding all sprite renderers in the scene and tell them to change color 1 Answer

Unity streching sprite gameobject to fit two positions. 1 Answer

Slicing sliced sprite via script 0 Answers

How can I change the Value of a Color through script? 1 Answer


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