Changing the color of a panel in code

Hi,
how can I change the color of a panel in code?
I can’t find a way to get to the panel’s color property :frowning:

a panel is mainly just a image. you just need to change the image color.

Thanks for your reply.
I tried to access the color via this.image.color = …, but the panel does not have a image property.

Ho else would I gain access to the image?
Could you provide me with a code snippet?

I’m confused. There is no panel script as part of the UI system so i dont know where you’d be doing a this.image.color. Image is a component so you’d need to get that component from the panel GO.

jbjbbborisb, gameObjects don’t have an already defined property for Image. You have to fetch it yourself, like this:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class PanelColorer : MonoBehaviour {

    Image panel;

    void Start(){
        //Right here. Make sure there's an image component attached,
        //or it will throw an error in Update()
        panel = GetComponent<Image>();
    }

    void Update(){
        panel.color = GetRandomColor();
    }

    Color GetRandomColor(){
        return new Color(Random.value, Random.value, Random.value);
    }

}
3 Likes