- Home /
Buttons become invisible when changing color (on Button or on Image) in script
I'm trying to make a UI that can add buttons to a panel and change their colors based on the player's progress in the game. I have a prefab button that I instantiate to create the list of buttons. Here's where I set up the buttons:
foreach(Quest q in QuestManager.questManager.quests){
GameObject buttonObj = Instantiate(questButtonPrefab) as GameObject;
buttonObj.transform.SetParent(gameObject.transform.GetChild(0), false);
RectTransform rt = buttonObj.GetComponent<RectTransform>();
rt.localPosition = new Vector3(0, buttonY, 0);
rt.sizeDelta = buttonSize;
buttonY -= (int)(buttonSize.y);
Button button = buttonObj.GetComponent<Button>();
button.onClick.AddListener(() => questButtonClicked(q.id));
button.transition = Selectable.Transition.None;
questButtons.Add(button);
Text buttonText = button.transform.GetChild(0).gameObject.GetComponent<Text>();
buttonText.text = q.questName;
Image buttonImage = buttonObj.GetComponent<Image>();
if(q.completed){
//button.colors = completedColorBlock;
buttonImage.color = completedColor;
} else if(q.started){
//button.colors = inProgressColorBlock;
buttonImage.color = inProgressColor;
} else if(q.isAvailable()){
//button.colors = availableColorBlock;
buttonImage.color = availableColor;
} else {
//button.colors = unavailableColorBlock;
buttonImage.color = unavailableColor;
}
}
When I try to change the colors, either on the image or the button components, the buttons disappear, but they still work if you click on where they should be. The Text object (child of the button) still displays. This only happens when I try to change the colors in the script, and works as expected when I change the colors in the editor while the game is running. Can anyone explain what is going on here?
I converted the comment to an answer. Please click the checkmark next to it to mark this question answered.
Answer by NoseKills · Feb 24, 2015 at 11:16 AM
If i remember correctly I've had similar problems because I think the Inspector likes to default the alpha of color variables to 0. If you make public Color variables, be sure to drag their alpha value up in inspector
$$anonymous$$y issue was the color$$anonymous$$ultiplier was set to zero as well!
Your answer
Follow this Question
Related Questions
Best Button for UnityGame 2D 1 Answer
Gui.Button Image in Button - error 1502 and 1503 0 Answers
UI Button multiple parameters 6 Answers
Image in Button is cut (New UI) 2 Answers
Transparent Images as UI 0 Answers