Author Topic: Change sprite on click  (Read 3777 times)

johnnydj

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
Change sprite on click
« on: July 27, 2014, 09:19:37 AM »
I was using NGUI 3.4.9 before, so I upgraded to 3.6.8 now.
I have a small script where on click I toggle between 2 sprites.

It worked fine in 3.4.9, but now when I click the button, the sprite changes, but as soon as I move the mouse
the sprite changes back to the first one.

I'm changing the sprite with this code:
  1. gameObject.GetComponent<UISprite>().spriteName = "music-off";

Any ideas? Something changed between these versions?
Thanks!

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Change sprite on click
« Reply #1 on: July 27, 2014, 09:23:07 AM »
Post the rest of the script. That snippet should work fine, so it's somewhere else the error lies. Where are you calling that line? Are you setting the SpriteName correctly? Is there a different script on the button that also sets the sprite to something? Maybe you have a UIButton on there, that sets the sprite in OnHover, which is messing with you. :)

johnnydj

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: Change sprite on click
« Reply #2 on: July 27, 2014, 09:33:18 AM »
The thing is, it worked perfectly fine with NGUI 3.4.9
No code was modified and I have no OnHover calls anywhere in my game.
I noticed that I can move the mouse on the button, but as soon as the mouse leaves the button, the sprite changes back.

  1. IEnumerator ButtonClick()
  2.     {
  3.         if (PlayerPrefs.GetInt("music") == 1)
  4.         {
  5.             //disable music
  6.             gameObject.GetComponent<UISprite>().spriteName = "music-off";
  7.             PlayerPrefs.SetInt("music", 0);
  8.             player.mute = true;
  9.         }
  10.         else if (PlayerPrefs.GetInt("music") == 0)
  11.         {
  12.             //enable music
  13.             gameObject.GetComponent<UISprite>().spriteName = "music";
  14.             PlayerPrefs.SetInt("music", 1);
  15.             player.mute = false;
  16.         }
  17.         pressAnimation.PlayQueued("Button");
  18.         yield return new WaitForSeconds(0.3f);
  19.         //gameObject.transform.localScale = new Vector3(1, 1, 1);
  20.     }

johnnydj

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 11
    • View Profile
Re: Change sprite on click
« Reply #3 on: July 27, 2014, 09:55:41 AM »
I managed to fix it like this:

  1. IEnumerator ButtonClick()
  2.     {
  3.         if (PlayerPrefs.GetInt("music") == 1)
  4.         {
  5.             //disable music
  6.             gameObject.GetComponent<UIButton>().normalSprite = "music-off";
  7.             PlayerPrefs.SetInt("music", 0);
  8.             player.mute = true;
  9.         }
  10.         else if (PlayerPrefs.GetInt("music") == 0)
  11.         {
  12.             //enable music
  13.             gameObject.GetComponent<UIButton>().normalSprite = "music";
  14.             PlayerPrefs.SetInt("music", 1);
  15.             player.mute = false;
  16.         }
  17.         pressAnimation.PlayQueued("Button");
  18.         yield return new WaitForSeconds(0.3f);
  19.         //gameObject.transform.localScale = new Vector3(1, 1, 1);
  20.     }

I just noticed in the new version of NGUI, the button component also has a Sprite section.
So switch the sprite in the UIButton component instead, works correctly :)

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Change sprite on click
« Reply #4 on: July 27, 2014, 05:57:31 PM »
Yeah, I believe it might have changed when it stores the "normal state"-sprite, so that would be the culprit. Good that you caught it.