NGUI : Cannot change NGUI Button sprite at Runtime when Pressing it.

Hello everyone,

I am actually stuck on something that should be quite simple but I cannot find any solution.

I am using NGUI, I have a GameObject named “Button - Sound” having UISprite, UIButton, properly set Box Collider on it. “sound_on_button” and “sound_off_button” are on the same UIAtlas.

The following script is attached to the GameObject :

[RequireComponent (typeof (UISprite))]
public class B_Sound : MonoBehaviour {

    private UISprite S_Sprite;
    bool cheat = false;

void Start ()
{
        S_Sprite = gameObject.GetComponent<UISprite>();
        S_Sprite.spriteName = (cheat) ? "sound_on_button" : "sound_off_button";
}
   
void OnPress (bool isPressed)
{       
    if (enabled && !isPressed)
    {
            cheat = !cheat;
            S_Sprite.spriteName = (cheat) ? "sound_on_button" : "sound_off_button";
       }   
}

    #region Debug
    #if UNITY_EDITOR || UNITY_REMOTE
    void Update()
    {
        if (Input.GetKeyUp("e"))
        {
            cheat = !cheat;
            S_Sprite.spriteName = (cheat) ? "sound_on_button" : "sound_off_button";
        }
    }
    #endif
    #endregion
}

The button Sprite is set on Start and GetKeyUp operations but not with OnPress (or sometimes when I click repeatedly on the Editor).

I hope you will have some clue for me, I am stuck on this for hours :confused:
Thanks in advance :slight_smile:

Finally got it fixed … I don’t know why but manage to make it working using :

gameObject.GetComponent<UIButton>().normalSprite

Hope it will be useful for someone.