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 PinkPumpkin · Aug 13, 2016 at 03:28 PM · colormaterial renderer

material.color colors the GameObject only partially

Hello,

I would like to change the color of the buildings (instantiate by clicking buttons), dependent if their current position is valid for placement or not (green= valid, red = invalid, white = placed)

I'm using following C# code:

 placeableBuilding.gameObject.GetComponentInChildren<MeshRenderer>().enabled = true;
 placeableBuilding.gameObject.GetComponentInChildren<MeshRenderer>().material.color = Color.red;

Same code for green and white.

But the building gets colored only partially. It is kinda weird for me because some areas with the same texture (roof) get colored, other areas not.

Also independent of the color, always the same area of the example building gets colored.

Can someone please give me a hint explain me what I'm doing wrong ?

alt text

alt text

green2.png (516.0 kB)
red22.png (286.5 kB)
Comment
Add comment · Show 1
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 NoseKills · Aug 13, 2016 at 04:16 PM 0
Share

$$anonymous$$y first guess would be that the mesh uses more materials than one and you'd need to loop through the .materials array to change them all.

The division between uncolored and colored areas looks weird though. $$anonymous$$ake certain there's no 2 buildings on top of eachother z-fighting

1 Reply

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

Answer by TheBusyBiscuit · Aug 13, 2016 at 04:33 PM

You shouldn't really access the materials by using renderer.material Because it may actually be the case that your Renderer has more than 1 Material.

Use renderer.sharedMaterials instead. And then loop through that array.

Now Material.color follows the same example, depending on your Shader, you can have multiple color variables. Look what Shaders the Materials of your Mesh use and then set their Colors by using

renderer.sharedMaterials[i].SetColor("_Color", color);

If your shader only uses one single color, using material.color would of course be fine as well. But if it does use multiple colors and you want to set all of them then just replace _Color with their Variable Names, you can find their Variable Names by opening the .shader file of your Shader. If you are using one of Unity's built-in shaders, _Color should work just fine, if not then download Unity's built-in shader package from the Download Archive https://unity3d.com/get-unity/download/archive and see yourself.

Also, it looks like you are instantiating a new building onto the same position as your last one... Don't do that, delete the existing building when instantiating a new one at that position or simply change the materials of your existing building instead of creating a new one.

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 PinkPumpkin · Aug 13, 2016 at 05:23 PM 0
Share

@Nose$$anonymous$$ills @mrCookieSlime

Thank you very much for your answers, I was able to fix it with your help!

The Prefabbuilding had multiple children that represented different areas of that building.

I wrote this code to get all of them:

             public Component[] components;
             components = placeableBuilding.gameObject.GetComponentsInChildren<$$anonymous$$eshRenderer>();
 
             for (int i = 0; i < components.Length; i++)
             {
                // e.g. color red
                components[i].gameObject.GetComponent<$$anonymous$$eshRenderer>().material.color = Color.red;
                 
             }

alt text

alt text

Thank you very much!

avatar image NoseKills · Aug 14, 2016 at 10:57 AM 0
Share

Yes you have to be careful not to accidentally duplicate materials by using material or materials[], but if you simply use shared$$anonymous$$aterials[i].SetColor("_Color", color); then all your buildings using that material will go green when you just want to highlight one.

If you want to $$anonymous$$imize draw calls, use shared$$anonymous$$aterials and pre-instantiate the colored versions of all needed materials and assign them to the shared$$anonymous$$aterials of the highlighted building on the fly.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I get my Buttons to switch colors? 0 Answers

Change button color to intermediate value on onclick() event 1 Answer

Unity - ScreenSize resolution problem 1 Answer

Help with this Camera Background Color Code 1 Answer

Unity2D: Remove Shader Colour 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