I have a row of buttons numbered 0 thru 9. The player may only activate 1 at a time. When a button is clicked, a OnClick method is called that changes the color of 3 different layered UI elements that shape the button (a parent “background”, the button itself, and a child text element). The currently selected button is cached to a static Button variable so that it can be reset to the default “unselected” colors when a different button becomes the active selection.
After the game has been started the OnClick method works perfectly.
private static Button activeWholeButton;
public void ApplyWholeRate(int value)
{
if (activeWholeButton != null)
{//reset the last active button to its default colors
Image activeButtonBackdrop = activeWholeButton.transform.parent.GetComponent<Image>();
Image activeButtonImage = activeWholeButton.GetComponent<Image>();
TextMeshProUGUI activeText = activeWholeButton.GetComponentInChildren<TextMeshProUGUI>();
activeButtonBackdrop.color = new Color32(0, 0, 0, 255); //black backdrop
activeButtonImage.color = new Color32(255, 255, 255, 255); //white button
activeText.color = new Color32(0, 0, 0, 255); //black text
}
//set the new active button colors and cache new active reference
Image backdrop = this.transform.parent.GetComponent<Image>();
Image buttonImage = this.GetComponent<Image>();
TextMeshProUGUI text = this.GetComponentInChildren<TextMeshProUGUI>();
backdrop.color = new Color32(255, 167, 66, 255); //orange backdrop
buttonImage.color = new Color32(60, 60, 60, 255); //black button
text.color = new Color32(255, 255, 255, 255); //white text
activeWholeButton = this.GetComponent<Button>();
//... do more unrelated stuff ...
}
The problem I’m unable to solve is this: I’m trying to ensure the game initializes with one of these buttons already “updated” with the active-selection-color-scheme and the reference to the cached button(so it properly returns to default colors when a new button is selected the first time).
I tried pasting the same exact code into a Start() method and using a separate Singleton to grab the reference gameObject(Button) needed to cache the static activeWholeButton variable. I placed debug.log lines all throughout the Start() block (which again, is identical to the OnClick method) and confirmed the code is running, the correct button object is referenced, and the colors are changing(according to the console). But when the game is done loading, the UI shows all 10 buttons with the same default unselected-color-scheme. Once I start clicking the buttons it all performs as desired.
What am I missing here? I even tried changing the “initializing method” from Start() to a boolean-checked LateUpdate() thinking there might be an order of operations issue, but no joy. Debug.log continues to report its working, but the interface is unchanged.