Changing Unity.UI.Panel Color
How would one go about doing that? My method did not work.
Image img = transform.Find("UpgradeCanvas/Speed/First").GetComponent<Image>().color = Color.blue;
It says it could not convert Unity.Color to Unity.UI.Image, but I am otherwise unsure of how to change this via script.
I can't believe I did that. Okay, so now I moved it down to img.color = color.red; but that still isn't actually changing the color. Any idea why?
test it :
Component img= transform.Find("UpgradeCanvas/Speed/First").GetComponent<Image>();
img.color = Color.blue;
is "UpgradeCanvas/Speed/First" a game object name ? :-?
It should be Image img, not component. It exists, so I don't know why it didn't work. I changed it to a public variable and just dragged it in, and now it works.
Answer by code_warrior · Jan 01, 2015 at 12:53 PM
Hi Marshal_Alessi,
I've just checked out your question in a real simply Unity Scene, I had one UI Panel Called MyPanel.
The working C# script:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class ChangePanel : MonoBehaviour {
// Use this for initialization
void Start () {
Image img = GameObject.Find("MyPanel").GetComponent<Image>();
img.color = UnityEngine.Color.red;
}
}
If you assign the script directly to your Panel you don't need the GameObject.Find() Method, simply call the GetComponent function.
All you now have to do is to change the color and the name of your object. Please note: Its essential to do the GetComponent and the process of assigning a color in two separate lines.
code_warrior
$$anonymous$$ake sure you actually have 'using UnityEngine.UI;' in your script; $$anonymous$$ine was missing and the code would not work until I spotted it.
Answer by DevsGoingViral · Jun 15, 2015 at 06:09 PM
Working js script if someone need:
#pragma strict
import UnityEngine;
import System.Collections;
import UnityEngine.UI;
///////////////////////
var imgObj : GameObject; //drag your Canvas here
var img : Image = imgObj.GetComponent.<Image>(); //drag your image here
function Start () {}
function Update () {
img.color = Color.red; //instant change
}
Answer by Jivibounty · Nov 24, 2018 at 11:36 PM
Your code assigns color to image. Hence cannot assign Unity.Color to Unity.UI.Image. That is the problem. remove "Image img =" .
THE CODE SHOULD BE
transform.Find("UpgradeCanvas/Speed/First").GetComponent().color = Color.blue;
AND NOT
Image img = transform.Find("UpgradeCanvas/Speed/First").GetComponent().color = Color.blue;
Look at the other answers. Green means the asker marked it as correct. It's more like what the person was asking. What you wrote could be a comment on that answer about doing it in one step ins$$anonymous$$d of 2.