- Home /
how to change material color for player customization.
i am wondering how to change the color of a material in a character customization scene, and then load that material color onto the character into the scene with the game play.
my character is a bird and it has seperate materials for each body part (the body parts are child objects of the whole character), so the body has a material called "player_body", beak has material called "player_beak" and so on but for the purpose of the question ill just focus on the body material and apply the fix (if we find one, hopefully) to the rest.
i have a script that detects which body part you want to change color, and what color to set that material to. ill just leave the parts of the script related to the body. (game is for android)
var PartOfBody; // sets what part of the body you want to change
function Update ()
{
if (Input.touchCount > 0)
{
var ray = Camera.main.ScreenPointToRay (Input.GetTouch(0).position);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit))
{
if(hit.collider.gameObject.name == "txt_Body")
{
Debug.Log("hit the body text");
PartOfBody = "Body";
}
// there are 3 colord boxes in my scene, if you touch one then it changes the color of that object to either red,blue,green.
if(hit.collider.gameObject.name == "Box_red")
{
Debug.Log("hit red box");
GameObject.Find(PartOfBody).renderer.material.color = Color.red;
}
if(hit.collider.gameObject.name == "Box_blue")
{
Debug.Log("hit blue box");
GameObject.Find(PartOfBody).renderer.material.color = Color.blue;
}
if(hit.collider.gameObject.name == "Box_green")
{
Debug.Log("hit green box");
GameObject.Find(PartOfBody).renderer.material.color = Color.green;
}
}
}
}
however theres a few problems
the color reverts after scene has stopped
Apparently the material gets cloned so the color change wont affect any other objects wearing the same material ("Player_body").
so is there a way to make the color change permanent (through playerprefs perhaps? not sure how to use playerfres with a color), And/ormake it so every object with the same material has the new color aswell?
thanks
Answer by CodeElemental · Feb 26, 2014 at 08:32 AM
I think you should work with
renderer.sharedMaterial
instead of
renderer.material.
Here's the documentation.
In terms of modifying the original material, I believe this works in the editor, but will not modify the material in a build. I believe if you want to modify the material across executions, you will need to save and apply the settings on the next execution. 'shared$$anonymous$$aterials' does change all the objects that share that material.
sharedmaterial works fine because the only 2 objects that are going to be using the same material is the model of the character in character custimation scene, and the model of the character in the game scene
I was trying the same.
It works only in the editor but not in build. Am I doing anything wrong ?
Answer by Hakumaru · Feb 26, 2014 at 08:58 AM
You could try setting variables in the Player info files, and have another file read those variable and set the material based upon that, so say "boots = 4" it takes that, and applies the proper texture for the boots, you do it on start up of the character, and something like "if(updateclothes)" and at the end of that if put "updateclothes = false;" so it updates the look, and then stop updating it, so whenever they put new gear on just put "updateclothes = true;" and upon switching of scenes, it should refer to the start, and update.