- Home /
Already solve the problem, nothing to do with texture issues :)
Change GUI.Button Texture on runtime
I want to change the texture of a button depending of the value of a variable:
StartCoroutine(DoublePrize());
if(doubleOptionA)
{
textureA = optionAActive;
textureB = optionB;
}
else
{
textureA = optionA;
textureB = optionBActive;
}
if(GUI.Button(optionAPos,textureA)) { doubleTries--; }
if(GUI.Button(optionBPos,textureB)) { doubleTries--; }
That's is inside OnGUI. Then I have the function DoublePrize with this code:
IEnumerator DoublePrize()
{
while(doubleTries > 0)
{
doubleOptionA = !doubleOptionA;
yield return new WaitForSeconds(1f);
}
}
I use Debug.Log to see if doubleOptionA is changing (I use it both on OnGUI and on DoublePrize()) and is changing good! What not is changing is the textures! I see always textureA = optionA and textureB = optionBActive. What I'm doing wrong?
EDIT: I was calling DoublePrize() from GUI! So the variable doubleOptionA was switching from true to false so fast that it seems to be always in the same state! I solve that an the problem is gone! :)
Answer by getyour411 · May 20, 2014 at 12:38 AM
Your GUI.Button code is only looking for a 'press', you need to wrap those in another if that shows one or other based on your conditional
Don't understand... I have the condition before showing the buttons!
The conditional you posted in your code has no effect on your if(GUI.Button) since it's not inside the { }
The if(...) you have wrapped around your buttons just listens for click events, which is why you were seeing both as you said in your OP.
Already solve my problem and edited the question! :) Thanks anyway!! :)