Author Topic: Changing sprite at runtime  (Read 1557 times)

pad406

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 62
    • View Profile
Changing sprite at runtime
« on: February 05, 2014, 10:56:52 AM »
I'm trying to change a sprite at runtime via a script. I change the UISprite.spriteName to what I want, and I can see them changed in the inspector. However, the sprite is not being displayed. If I pause the game and manually select the same sprite name from the Atlas, then un-pause the game, it has updated. I've tried various combinations of .MarkAsChanged() .Update() to no avail. Any ideas on what I'm missing

thanks
Paul

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Changing sprite at runtime
« Reply #1 on: February 05, 2014, 02:28:42 PM »
Setting UISprite.spriteName is all you have to do, no need to call MarkAsChanged. That's what the UIImageButton script does as well. So what you're seeing is that it changes in inspector, but does not change in the game window? Can you post some code of what you're doing?

pad406

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 62
    • View Profile
Re: Changing sprite at runtime
« Reply #2 on: February 05, 2014, 04:56:37 PM »
Hi Aren

I've got it working, but I'm not sure why. For some reason I decided to try doing a .Trim() on the name when I set it and it works. I have checked via Debug.Log and my usual ">" + stringName + "<" and there's no spaces to be trimmed. I'm going to dig a bit more into it and if I find anything that may be of interest I'll post another reply.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Changing sprite at runtime
« Reply #3 on: February 05, 2014, 05:03:53 PM »
Trim() removes more than just spaces. Tabs, new line characters, other invisible characters... Try printing the before and after length.

pad406

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 62
    • View Profile
Re: Changing sprite at runtime
« Reply #4 on: February 05, 2014, 05:11:15 PM »
Yup, carriage  return at the end.