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 zafadadynamic · Apr 02, 2014 at 05:01 AM · materialcolormaterial colorrgba

Changing Material Color Via Script

So I think I actually have a simple problem. I have this script portion of a script:

//Stretch arrow. if (Distance < MaxPull) {

 var r : int = (255 / MaxPull * Distance);
 var b : int = (255 - (255 / MaxPull * Distance));
 
 ActualArrow.renderer.enabled = false;
 MaterialColor.SetVector("_Color", Vector4(r, 0, b, 1)); 
 ActualArrow.renderer.enabled = true;
 WholeArrow.animation["Arrow Animation"].time = Distance * (WholeArrow.animation["Arrow Animation"].clip.length / MaxPull);
 WholeArrow.animation["Arrow Animation"].speed = 0.0;
 WholeArrow.animation.Play("Arrow Animation");
 print(MaterialColor.GetColor("_Color"));
 

}

I'm trying to change the color of an object based on how far the mouse cursor's ray is away from it. Anyway, I went through a billion different problems. At first I was trying to animate the colors which didn't seem to work and gave the object a pink color. In fact, that's what's happening now, but I checked the material color after pausing the game and the numbers, instead of 0 - 255, are up in the thousands.

The variables you see above, r and b, are ALWAYS between 0 - 255. Mathematically it's impossible for them to be above or below. So I figured maybe the material was taking float values like 149.652 and using them as ints by removing the decimal point.

SO I went and isolated the variables, made them an int, did a print to the console thingy, and still no go for some reason. The values are ridiculously high. That's why the color becomes this pink color (I'm colorblind so it very well could be purple).

To put everything into a nutshell:

Need a value between 0 - 255 which seems to be working.

Print returns values such as 149.000 now that everything's int (Maybe those zeros are a problem?).

Object becomes pink and the material RGB values while checked in the inspector are high as f.

I tried various methods of changing the color as well.

I know there are a few bugs with changing materials or something in the new Unity so I don't know if that's it or not. I hope I gave you enough information and that I'm not asking a really simple question :)

Thanks in advance, as always.

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

1 Reply

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

Answer by robertbu · Apr 02, 2014 at 05:30 AM

A couple of comments. The RGB values the Color class are float values between 0.0 and 1.0, not values in the range of 0 to 255. There is a Color32 class in Unity that takes colors in that range, but the shader takes the Color class.

In addition, since you are trying to assign to _Color, you can the shortcut to directly assign the color.

 MaterialColor.color = new Color(0.5, 0.0, 0.75, 1.0);

Actual color changes will depend on the shader.

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 zafadadynamic · Apr 02, 2014 at 02:49 PM 0
Share

I $$anonymous$$NEW it was something simple. I had to change the equations around to deal with 0 - 1 rather than 0 - 255. All I had to do was this:

 var r : float = ((1.0 / 255) * (255 / $$anonymous$$axPull * Distance));
                         
 var b : float = ((1.0 / 255) * (255 - (255 / $$anonymous$$axPull * Distance)));

Anyway, can you please rewrite your comment as an answer so I can accept it?

P.S. I always hate it when I post something on Unity Answers and I feel like I just wasted everyone's time afterward lol

Thanks for your help. It's greatly appreciated.

avatar image zafadadynamic · Apr 02, 2014 at 10:52 PM 0
Share

Thanks again

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

20 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

Related Questions

Material doesn't have a color property '_Color' 4 Answers

Changing two different objects renderer colour 1 Answer

What is the best way to implement the ability to allow multiple colors on a duplicated mesh? 1 Answer

How to get color from a Tree for Tree Instance? 0 Answers

weird color or lighting bug in Unity? 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