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 Orloffyeah · May 12, 2013 at 11:59 PM · collisioncolorrendererdetect

Detect Color in "If" Statement

Hi, I´m trying to make a system in which I detect the color of a platform I hit, but it doesn't seems to work, how can I detect var + renderer.material.color?

Code:

 void FixedUpdate () {
 
         if(touchingPlatform && renderer.material.color(210,2,2)){
             transform.localPosition = startPosition;
         
         }
     }
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
3
Best Answer

Answer by greenshadow · May 13, 2013 at 02:30 AM

I am pretty sure that statement is just going to set the color, not compare it. You might try something like this:

 Color myColor = new Color(210f, 2f, 2f, 1f);
 if(touchingPlatform && renderer.material.color == myColor){
     transform.localPosition = startPosition;
 }
Comment
Add comment · Show 3 · 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 Orloffyeah · May 13, 2013 at 04:11 AM 0
Share

Thanks for the reply, but it isn't working, it doesnt detect when I touch something of that color. Have a nice day :)

avatar image greenshadow · May 13, 2013 at 04:37 AM 0
Share

Ahh.. of course. I forgot, Unity does not use a scale of 0-255 for it's RGBA scheme; it converts them to a value between 0.0 and 1.0 (with 1.0 equaling 255). For example, I have a blue object in a scene. The Unity RGBA value for this is (0f, 0f, 1f, 1f). In the update method for the attached script, I execute this check:

 Color myColor = new Color(0f, 0f, 1f, 1f);
 if (this.gameObject.renderer.material.color == myColor) {
     //Works every time the object is blue
 }

This triggers just fine. To really make sure you are making the proper comparison, you should have that color stored in a variable when you assign it to an object. You also need to make sure you are accessing the renderer of the correct gameObject. Then you can make the comparison like so:

 if(touchingPlatform && gameObject.renderer.material.color == myStoredColor){

 }
avatar image Orloffyeah · May 13, 2013 at 05:02 AM 0
Share

Thanks, it WOR$$anonymous$$S! :) Now I can move on in my project, thanks for the quick replies and the excellent feedback. Have a nice day :)

avatar image
1

Answer by AlucardJay · May 13, 2013 at 04:20 AM

The values of the Color variable are between 0 and 1, not 0 and 255 :

http://docs.unity3d.com/Documentation/ScriptReference/Color.html

Each color component is a floating point value with a range from 0 to 1

Apart form that, greenshadow is correct, you want to check if your renderer is equal to somthing :

 if ( touchingPlatform && renderer.material.color == Color(210,2,2) ) {
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 greenshadow · May 13, 2013 at 04:39 AM 0
Share

Good catch my friend.

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

16 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

Related Questions

Changing two different objects renderer colour 1 Answer

Detecting the Green in a Material's Color on Collision 1 Answer

Remove color of an object 2 Answers

Changing a Game Object's RGBA Value on Collision (JS) 1 Answer

Change material color of array objects 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