Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Resolved Changing background color for ui toolkit in code

Discussion in '2022.2 Beta' started by MarexDS, Sep 18, 2022.

  1. MarexDS

    MarexDS

    Joined:
    Oct 31, 2021
    Posts:
    2
    Code (CSharp):
    1. itemList.bindItem = (item, index) =>
    2. {
    3.     item.Q<Label>("itemName").text = allItemData[selectedType][index].name;
    4.     Color color = new Color();        
    5.     switch(allItemData[selectedType][index].rarity)
    6.     {
    7.         case Rarity.Type.Common:
    8.             color = new Color(230, 220, 220, 1);
    9.             break;
    10.         case Rarity.Type.Rare:
    11.             color = new Color(66, 144, 245, 1);
    12.             break;
    13.         case Rarity.Type.Epic:
    14.             color = new Color(83, 75, 116, 1);
    15.             break;
    16.         case Rarity.Type.Legend:
    17.             color = new Color(161, 95, 34, 1);
    18.             break;
    19.     }
    20.     Debug.Log(color);
    21.     item.Q<VisualElement>("background").style.backgroundColor = color; //This always sets color to white
    22.     //item.Q<VisualElement>("background").style.backgroundColor = Color.red;  //This works, it makes it red
    23. };
    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?
     
  2. MiTschMR

    MiTschMR

    Joined:
    Aug 28, 2018
    Posts:
    374
    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.
     
    sosodev likes this.
  3. MarexDS

    MarexDS

    Joined:
    Oct 31, 2021
    Posts:
    2