- Home /
Change the button target image alpha
Hello guys and gals so my head hurts and I need ya help.
Im trying to change this deactivated buttons alpha, shoot even its multiplier to...anything at this point but at runtime it just refuses to change please send help, and tylenol.
if (this.transform.parent.GetComponent<_GenericCardBehavior_>().ofPlayer != true)
{
for (int i = 0; i<= (children-1); i++)
{
ColorBlock color = this.transform.GetChild(i).GetComponent<Button>().colors;
this.transform.GetChild(i).GetComponent<Button>().interactable = false;
color.colorMultiplier = 3;
}
}
S.N. this in c#
Answer by MrRightclick · Feb 05, 2019 at 01:44 PM
The problem with your script is that you're creating a new variable ColorBlock named "color", then assigning a copy (not a reference!) of the button's colorblock to this new variable, then changing this new variable. You're not actually changing the color of the button at all.
Try instead
for (int i = 0; i<= (children-1); i++)
{
// Find the button, assign it to a variable so we don't have to do this again for every thing we do to the button
Button but = this.transform.GetChild(i).GetComponent<Button>();
// Create a new alpha
float newAlpha = 40f;
// Change the color of the button
but.color = new Color(but.color.r, but.color.g, but.color.b, newAlpha); // Here we just set a new alpha and use the current colors of the button
but.interactable = false;
}
Note: Just setting a button uninteractable should usually be enough to change the Alpha setting if you have the button's colors set up correctly in the Inspector.
I just got to this conclusion was about to put my own answer XD thanks i was about to get depressed cause no one responded for a while lmao you sit get a beer.