Search Unity

Saving shaders new color.

Discussion in 'Editor & General Support' started by SamohtVII, May 4, 2019.

  1. SamohtVII

    SamohtVII

    Joined:
    Jun 30, 2014
    Posts:
    370
    I have a car that is a prefab and have this code to change the car color in the garage...

    Code (CSharp):
    1. public static Vector4 hexColor(float r, float g, float b, float a){
    2.          Vector4 color = new Vector4(r/255, g/255, b/255, a/255);
    3.          return color;
    4.      }
    5.  
    6.     public void chageTheCarColor(string stats) {
    7.         Color newColor = hexColor(int.Parse(stats.Substring(0,3)), int.Parse(stats.Substring(3,3)), int.Parse(stats.Substring(6,3)), 255);
    8.         theCar[currentCar].GetComponent<Transform>().GetChild(1).GetComponent<Renderer>().material.SetColor("_Color", newColor);
    9.     }
    That works fine and changes the color in the garage. How can I make the shader remember the new color permanently? As soon as I load the next level and leave the garage the old shader color is back. Do I need to run a line of code to resave the shader or prefab? Or do I save the car color as a playerpref and set the color at the start of each scene load?

    Thanks.