- Home /
If statement breaking button color copy
I am trying to create a condition to check for an image shown with a button click as there are 12 different buttons to change colors. Without an if statement, all 12 buttons get the same color.
The First script works great without the if statement and copies the color from a button to another button. However, when I add in the if statement the color will not copy from a button to another button. With an if statement, all 12 buttons are to get different colors.
This is the code working correctly:
public GameObject btn2;
// to set the color
[SerializeField]
public Color ColorToSet = Color.white;
public void ColorToSetBtnPressed()
{
ColorToSet = GetComponent<Image>().color;
GameObject btn2 = GameObject.Find ("btn2");
btn2.GetComponent<Image>().color = ColorToSet;
}
Here is the code with the If Statement that is not working
public GameObject btn2;
public bool selbtn2 = false;
// to set the color
[SerializeField]
public Color ColorToSet = Color.white;
public void ColorToSetBtnPressed()
{
ColorToSet = GetComponent<Image>().color;
if (selbtn2 == true) {
GameObject btn2 = GameObject.Find ("btn2");
btn2.GetComponent<Image>().color = ColorToSet;
}
I need to be able to set all 12 buttons with their own different color with just clicking on one button after changing colors.
I figured it out. I had to change my if statement from if (selbtn2 == true) to if (selbtn2.active). This now picks the single button to copy the color and apply it to another button.
Your answer
Follow this Question
Related Questions
UI button doesn't appear - c# 3 Answers
Weird Boolean Problem c# 1 Answer
Unable to show gameobjects at specific timing 0 Answers
Multiple Button Modifications 2 Answers
UI Button color 1 Answer