Changing background color for ui toolkit in code

itemList.bindItem = (item, index) =>
{
    item.Q<Label>("itemName").text = allItemData[selectedType][index].name;
    Color color = new Color();         
    switch(allItemData[selectedType][index].rarity)
    {
        case Rarity.Type.Common:
            color = new Color(230, 220, 220, 1);
            break;
        case Rarity.Type.Rare:
            color = new Color(66, 144, 245, 1);
            break;
        case Rarity.Type.Epic:
            color = new Color(83, 75, 116, 1);
            break;
        case Rarity.Type.Legend:
            color = new Color(161, 95, 34, 1);
            break;
    }
    Debug.Log(color);
    item.Q<VisualElement>("background").style.backgroundColor = color; //This always sets color to white
    //item.Q<VisualElement>("background").style.backgroundColor = Color.red;  //This works, it makes it red
};

Trying to make an editor window to create and edit scriptable objects, everything works fine so far except when trying to change background color for elements in a list, it’s always white even tho in debug log it looks like it switched successfully. Is this bugged or am I doing something wrong?

You may be using the wrong constructor: https://docs.unity3d.com/ScriptReference/Color-ctor.html

The one you have only accepts values from 0 to 1 in float.

1 Like

yes that was it, thank you!