Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Tofudude624 · Dec 14, 2015 at 11:03 PM · materialmaterial color

Material instance won't change color?

I'm trying to and change the color of a trail renderer. I created this workaround, but I'm running into an odd issue. The material instance's color is changed in the Unity inspector, but it won't change color in the game unless I manually use the color picker and change the color (and sometimes, even that does nothing). What am I doing wrong?

 var Test: Material;
 var MyColors: Color[];
 
 function Start(){
     Test = GetComponent.<TrailRenderer>().material;
     Test.SetColor ("_TintColor", MyColors[1]);
 }
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
1

Answer by LazyElephant · Dec 15, 2015 at 12:37 AM

If you remove the . before the <TrailRenderer> and reformat the variables and start function to c#, this does run as a c# script. I'm not too familiar with javascript, but after checking the Unity reference page for GetComponent, it appears that the javascript version uses the syntax GetComponent( string name) instead of GetComponent<component>(). To get your code working, I changed it to the following

 var test: TrailRenderer;
 var myColor: Color;
 
 function Start () {
     test = GetComponent("TrailRenderer");
     test.material.SetColor("_TintColor", myColor);
 }


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 Bunny83 · Dec 17, 2015 at 04:46 AM 1
Share

No, not really. You shouldn't use the string version in any language as the return type will be Component in that case. UnityScript will automatically cast it if you pass it to a typed variable, but the string version is much slower than the generic or the type inferred version.

The generic syntax is perfectly fine. It looks like this in UnityScript. The code in question should compile without any errors. However in UnityScript it's usually better to use the type-inferred version. So ins$$anonymous$$d of:

 Test = GetComponent.<TrailRenderer>().material;

you can do:

 Test = GetComponent(TrailRenderer).material;

In UnityScript passing a "type" as parameter will implicitly pass typeof(type). Furthermore the compiler inferes the return type from the given parameter even though it's not a generic method. It's a feature of the UnityScript compiler. Though both versions should work.

Apart from pure syntax, there could be other problems which usually should result in some kind of runtime error or warning.

  • The code sets the color only once when the game is started. Any change of the myColors array while the game is running won't affect the trail renderer.

  • You always set the color to the second color in the array (index 1). So why using an array in the first place? $$anonymous$$aybe it's just an example, but it just looks strange. Also make sure you have at least two colors in the array, otherwise you get an out of bounds error.

  • You set the shader property called "_TintColor". Only a few shaders use this property, usually the particle shaders. If you use a different shader that doesn't have that property of course your code will have no effect.

avatar image LazyElephant Bunny83 · Dec 17, 2015 at 05:10 AM 0
Share

I'm going to assume it was my general lack of Unityscript knowledge that caused it, but when I tried using his code, I was unable to get it to work, even with the proper shader attached. However, after reading your comment and trying again, I was able to get his code to work as is. I'm not sure what i did wrong the first time.

Every time you post something, it's always very in depth and I learn something new. $$anonymous$$eep up the good work. You should post this comment as an answer, since it's more accurate than $$anonymous$$e.

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

33 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

Related Questions

can i apply an ad on a material 1 Answer

Changing color on specific tile in texture at runtime 0 Answers

Can I use MaterialPropertyBlock to set properties per material instead of per renderer? 0 Answers

Fading not working out as intended 0 Answers

Transparent texture color? 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