Search Unity

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

Discussion in 'Editor & General Support' started by mickee, Jun 17, 2017.

  1. mickee

    mickee

    Joined:
    Dec 26, 2013
    Posts:
    7
    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 :

    Code (CSharp):
    1. [RequireComponent (typeof (UISprite))]
    2. public class B_Sound : MonoBehaviour {
    3.  
    4.     private UISprite S_Sprite;
    5.     bool cheat = false;
    6.  
    7. void Start ()
    8. {
    9.         S_Sprite = gameObject.GetComponent<UISprite>();
    10.         S_Sprite.spriteName = (cheat) ? "sound_on_button" : "sound_off_button";
    11. }
    12.    
    13. void OnPress (bool isPressed)
    14. {      
    15.     if (enabled && !isPressed)
    16.     {
    17.             cheat = !cheat;
    18.             S_Sprite.spriteName = (cheat) ? "sound_on_button" : "sound_off_button";
    19.        }  
    20. }
    21.  
    22.     #region Debug
    23.     #if UNITY_EDITOR || UNITY_REMOTE
    24.     void Update()
    25.     {
    26.         if (Input.GetKeyUp("e"))
    27.         {
    28.             cheat = !cheat;
    29.             S_Sprite.spriteName = (cheat) ? "sound_on_button" : "sound_off_button";
    30.         }
    31.     }
    32.     #endif
    33.     #endregion
    34. }
    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 :/
    Thanks in advance :)
     
  2. mickee

    mickee

    Joined:
    Dec 26, 2013
    Posts:
    7
    Finally got it fixed ... I don't know why but manage to make it working using :
    Code (CSharp):
    1. gameObject.GetComponent<UIButton>().normalSprite
    Hope it will be useful for someone.