RGB Color

What I want:

  1. When I press ‘1’ a material shuld be black
  2. When I press ‘2’ a material shuld be white
  3. When I press ‘3’ a material shuld be “a skin color (given in RGB value)”
    (It’s all ONE material / SAME material)

When I press 1 or 2 I get the result I want.

My problem when I press 3:

  1. All shadows disapear on my gameObject
  2. The primary color is white (not a skin color, that I want)
  3. The secondary color is kinda what I want but the RGB values are wrong
  4. When I stop my program the material on my player is not the same as when the program started.
    4.1) Exampel: I start my program → The material is black → I press 2 and the material becomes white → I stop the program → My material is still white and not black as it was in the start.
public class ColorChange : MonoBehaviour
{
    [SerializeField] private Material playerSkin;

    public void Update()
    {
        if (Input.GetKeyDown(KeyCode.Alpha3))
        {
            //-----------My 1 Idé--------------//
            Color test = new Color(217, 130, 52);
            playerSkin.SetColor("_Color", test);

            //-----------My 2 Idé--------------//
            playerSkin.color = new Color(217, 130, 52);
        }

        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            playerSkin.SetColor("_Color", Color.black);
        }

        if (Input.GetKeyDown(KeyCode.Alpha2))
        {
            playerSkin.SetColor("_Color", Color.white);
        }
    }
}

Photographs of code are not a thing. If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

You may edit your post above.

As for the actual problem, please refine your description. Are you just unable to use the editor to set color, or are we talking about code? Remember, we cannot read your mind: you must explain everything.

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

Unity’s Color values are from 0-1 (not 0-255). You need to convert.

Basically, you’d just need to divide by 255, i.e.
Color test = new Color(217f/255f, 130f/255f, 52f/255f);

4 Likes

As above, please look at the docs which show you the ranges: Unity - Scripting API: Color

I have done some changes, hope it’s beter now :slight_smile:

Thx kdgalla and MelvMay for your help :smile: and the problem 1 - 3 is solved. But how can I fix 4?

Add code in OnApplicationQuit to set the material color back to whatever you want.

https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationQuit.html

Edit: note that this permanent material change only happens in the editor. It’s actually updating the material asset. When you create a build, though, then the color will always be the same when the application starts.

What do you mean?

Also:

  • Why does the material not go back to the original?
  • Is there eny more things that does not go back to the original when the program stops?

Because the material that you reference is an asset in your project. In a build game the asset would go back to the state it was when you restart your game as assets do not change permanentally in a build game. However inside the editor assets can be changed and those changes persist.

As I just said, only inside the editor, any assets you’re referencing. When you create a copy at runtime you can change that copy and it won’t affect the asset. This is for example what the Renderer.material property does. When you use the sharedMaterial and there is an asset assigned, changing that material would permanentally change that material asset inside the editor. Again, in a built game no asset can be changed permanentally as they are compiled into the game files when you build the game and those are essentially read only. You can still change them in memory but when you restart your game they would always be in the initial state that they had when you build the game.

ps: About your original problem with the color, Unity also has a Color32 struct. It actually contains 4 byte values instead of 4 float values (so it’s 4 times smaller). Color32 uses the usual byte ranges 0 - 255. Unity has implicit type conversion operators implemented for those types. So in most cases you can directly assign a Color32 value to a Color value and it would be converted automatically. There are rare cases where the implicit conversion may result in ambiguity that the compiler can not solve and throws an error. However a manual cast would always work.

Stupid example

Just as an example

void Method(int i,  Color c)
{
}

void Method(float f, Color32 c)
{
}

Having such overloads would cause issues when you do

Method(5, new Color32(128,128,128,255));

Since both arguments can be implicitly converted to the other argument, the compiler does not know if it should use the first overload and convert the int to a float, or use the second overload and convert the Color32 to a Color. Using an explicit cast would solve that ambiguity:

Method(5, (Color)new Color32(128,128,128,255));

or

Method(5f, new Color32(128,128,128,255));

Ah, thx for all the help. Have a good evening :smile: