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.. At Runtime the S_player Color Disappears.. 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.
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.
what is your suggestion then bud?
because im still learning... as i go along.
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!
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
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.
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);
}
}