• 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
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

People who like this

0 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 sharedMaterial.

SharedMaterial is the one actually shared between all objects using this material.

When you access material, Unity will automatically create a copy of sharedMaterial. (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 sharedMaterial 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
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
Mikilo
kareemhammad
Solidsoldier_

People who like this

3 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

UI appearing in direction of a game object 0 Answers

How to Switch GameObjects (Weapons) by reaching a specific amount of score? 0 Answers

How can I move a gameobjects box Collider with another asset? 0 Answers

Problem instantiating projectiles 1 Answer

How to reset a child object to its parent 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