Problems with Colors in Unity 2D

Hey! I’m relatively new to Unity and I’m having some trouble with changing colours of game objects that I can’t find explanations for online.

The first one is pretty simple: when i try to assign a color to an object via script:

GetComponent<Renderer>().material.color = new Color32(...);

I always get a darker color than the rbg code I chose from the inspector.
So if I chose new Color32(0,255,227,255); , i would still get blue but a much darker one.
Same thing happens if I try to get random colours, they all seem to be the same each time, and very dark.
I already tried creating a public Color in the script and selecting the one i wanted via inspector to no luck. Using HSV and just Color() also hasn’t worked.

The second one is a bit more tricky, i’m running this:

void OnTriggerEnter2D(Collider2D collision){
 GetComponent<Renderer>().material.color= collision.gameObject.GetComponent<Renderer>().material.color;
}

To make the player get the colour of the Triggering object when he triggers it, but nothing changes at all.

I’ve looked everywhere but couldn’t find a solution to this, maybe i’m just missing something very obvious. Thank you for your time!

Hello @TheKidsDontStandAChance ,
What kind of material/shader are you using on the object you are trying change color on?
Also, what color space is your project in? (Edit > Project Settings > Player > Color Space)

The material is just Sprites-Default and the color space is Gamma.

Is your renderer a SpriteRenderer?
If that is the case, try to use SpriteRenderer.color (Unity - Scripting API: SpriteRenderer.color) instead.

As to why your code is creating a darker result, I can’t reproduce this when using your code. You could share some screenshots to show what you are seeing as well, that might give me some more insight.

Well, using SpriteRenderer.color fixed the OnTriggerEnter problem, thank you!
I’ll have a go at the first one now.

1 Like