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 Custardcs · Aug 31, 2015 at 12:27 PM · gameobjectschange material

Change Color of Material on Multiple Objects..

Hey I Have been searching all Night, for something to help me and have tired about a million combinations and i cannot seem to get what i am looking for..

Please mind that i am also new to C#, but any help will be appreciated.

ugh alright I have a Script Player.cs.

 using UnityEngine;
 using System.Collections;
  
 public class Player : MonoBehaviour
 {
    
     public string username;
     public bool human;
     public Color userColor = new Color(1,1,1,1);
  
  
  
     // Use this for initialization
     void Start ()
     {
         PlayerColor();
     }
  
     //PlayerColor
     void PlayerColor()
     {
         Renderer rend = GetComponent<Renderer>();
         rend.material.shader = Shader.Find("PlayerColor");
         rend.material.SetColor("_Color", userColor);
         }
        
     }

This is before i run... you can see everything is assigned.. alt text At Runtime the S_player Color Disappears.. alt text But if i drag it back on the material it is picking up the color that the player selected..

But Its Only Changing the Color that the material is assigned to if that material is on another Object it does not change it at all.

beforeruntime.jpg (42.0 kB)
runtime.jpg (46.9 kB)
Comment
Add comment · Show 5
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 Mikilo · Aug 31, 2015 at 12:54 PM 0
Share

You have a bit misunderstood the difference between material and shared$$anonymous$$aterial.

Shared$$anonymous$$aterial is the one actually shared between all objects using this material.

When you access material, Unity will automatically create a copy of shared$$anonymous$$aterial. (You can see this in the inspector, it adds "(Instance)" at the end of the name).

And then you have a new material used only by your object. Therefore be careful when you use material, you might create many unwanted new materials.

avatar image Custardcs · Aug 31, 2015 at 03:30 PM 0
Share

what is your suggestion then bud?

because im still learning... as i go along.

avatar image Mikilo Custardcs · Aug 31, 2015 at 03:54 PM 0
Share

You should answer with a comment not a real Answer.

Anyway, I was just explaining how shared$$anonymous$$aterial and material work. To make you understand more about those 2 fields.

If you have found the solution, it's good!

avatar image Custardcs · Aug 31, 2015 at 04:01 PM 1
Share

Oh sorry bro lol still learning :)

well thanks, so far its working well.. there is still alot of bumps ahead of me... :/ but ill get there with dedication and help :D

avatar image Mikilo · Aug 31, 2015 at 04:06 PM 1
Share

You are welcome, we are all here for that! To learn! :D

Also if have found the answer, you should convert your comment into an Answer.

It makes the topic clear and neat.

1 Reply

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

Answer by Custardcs · Aug 31, 2015 at 08:08 AM

I found the fix

so sharedMaterial is used for everything using that material while material is just the game object applied to it? thank you i can see it starts

Wow thanks Bud i found the problem with the shader...

here is the code that works..

 using UnityEngine;
 using System.Collections;
  
 public class Player : MonoBehaviour
 {
    
     public string username;
     public bool human;
     public Color userColor = new Color(1,1,1,1);
  
  
  
     // Use this for initialization
     void Start ()
     {
         PlayerColor();
     }
  
     //PlayerColor
     void PlayerColor()
     {
         Renderer rend = GetComponent<Renderer>();
         rend.sharedMaterial.shader = Shader.Find("Custom/S_PlayerColor"); // this is where the issue was had to add custom.
         rend.sharedMaterial.SetColor("_Color", userColor);
         }
        
     }
           





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

Is it safe to instantiate a GameObject and then change it's components? 1 Answer

The pickups aren't rotating on the spot, but around the screen. My player game object also isn't receiving input from my keyboard. What is going on? 0 Answers

Line Renderer Positions go blank now and then in Inspector for no obvious reason 0 Answers

List resulting in out of range 0 Answers

Changing Second Material 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