Uri Popov
Uri Popov

Reputation: 2177

Change the color of SpriteRenderer in Unity3d

I have this method that picks a random color from a preset array of colors.

 public Color GetRandomColor()
 {
     return colors[rand.Next(0, colors.Length)];               
 }

Then I use it in this method in another class

public void RandomizeColors()
{
    for(int i = 0; i< spriteRenderers.Length; i++)
    {    
        spriteRenderers[i].color = rColors.GetRandomColor();
    }
}

The problem is that the colors of the sprite renderers all get set to some strange values. The RGB gets set to something in the thousands.

if I use spriteRenderers[i].color = Color.black for example it works fine.

I have checked the return values of GetRandomColor() and they are all correct. Where am I doing this wrong ?

Edit: The array of colors. This is part of a constructor:

colors = new Color[7];
colors[0] = new Color(87f,72f,161f) ;
colors[1] = new Color(39f,145f,221f);
colors[2] = new Color(233f,191f,57f);
colors[3] = new Color(238f,133f,57f);
colors[4] = new Color(238f,71f,46f);
colors[5] = new Color(193f,57f,235f);
colors[6] = new Color(104f,176f,58f);

Upvotes: 0

Views: 2773

Answers (2)

Fr3ak1n0ut
Fr3ak1n0ut

Reputation: 71

Directly extracted from the Unity Scripting API:

Color

struct in UnityEngine

Description

Representation of RGBA colors.

This structure is used throughout Unity to pass colors around. Each color component is a floating point value with a range from 0 to 1.

Here is the Link to the Documentation: Unity Scripting API

Upvotes: 2

Nika Kasradze
Nika Kasradze

Reputation: 3019

Unity Documentation says you need to have the r, g and b arguments in the range of 0f to 1f. So you can basically divide your r, g and b by 256.

Upvotes: 3

Related Questions