- Home /
Can't access image color
This is so weird because Visual Studio says that Image does not contain a definition for color, but clearly it does.
Here's the whole code: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class RadialMenu : MonoBehaviour
{
public Transform radialMenu;
public List<RadialButton> buttons = new List<RadialButton>();
private Vector2 mousePos;
private int selectedItem;
private int oldItem;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
UpdateMenu();
}
void UpdateMenu()
{
//Gets the mouse position between 0,1
mousePos = new Vector2(Input.mousePosition.x / Screen.width, Input.mousePosition.y / Screen.height);
//Gets the angle the mouse is in
float angle = ((Mathf.Atan2(.5f, 0) - Mathf.Atan2(mousePos.y - .5f, mousePos.x - .5f)) * Mathf.Rad2Deg) % 360;
selectedItem = (int)(angle / (360 / buttons.Count));
if(selectedItem != oldItem)
{
buttons[oldItem].image.color = buttons[oldItem].normalColor;
}
}
}
public class RadialButton
{
public string name = "Button" + Random.Range(0, 100);
public Image image;
public Color normalColor = Color.gray;
public Color highLightedColor = new Color(.4f, .4f, .4f, 1);
}
Are you sure you don't have a custom class / struct called Image
in your project?
Right click on the Image
word in Visual Studio then > Go to definition. If it's not Unity's image, you will have to specify the complete name of the class to avoid confusion:
public UnityEngine.UI.Image image;
Yeah, that was the problem, thanks so much
Answer by unity_Vpi53N_fb99F6g · Apr 28, 2020 at 06:44 PM
The problem was I had another class called Image.
Answer by GrayLightGames · Apr 02, 2020 at 05:21 AM
Debug.Log may help you solve this... first I would check the value of oldItem (it should be zero unless you initialized it somewhere else). Then I would check if buttons[oldItem] is null. Then check which button it is by checking the name. Then check if the Image attached to buttons[oldItem] is null, and maybe even check image.name to see if it has the intended Image object attached. I copied your code to an empty script, it didn't throw an error so I'm assuming it's giving you an error at runtime. Hope that helps!
Thanks so much for the response, but the error is not at runtime, although you did save me a lot of trouble since the list was null, and that would've given me a lot of errors later on.