Author Topic: Changing a sprite in code  (Read 2161 times)

ENAY

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 248
    • View Profile
Changing a sprite in code
« on: May 17, 2012, 11:47:28 PM »
Ok, stupid question alert. How do I change a sprite into another? In code all I want to do is choose another sprite in the atlas.

Put simply, I want something like this.

MyNGUIMenu.GetComponentsInChildren<UISprite>()[0].image = "image2";


This is for a character select screen, I have my window, multiple faces in the atlas, just need to find that last piece of the puzzle to change the face.

dlewis

  • Guest
Re: Changing a sprite in code
« Reply #1 on: May 18, 2012, 12:52:28 AM »
You will want to change the sprite name.

UISprite mySprite.spriteName = "newSprite";
« Last Edit: May 18, 2012, 01:08:22 AM by dlewis »

ENAY

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 248
    • View Profile
Re: Changing a sprite in code
« Reply #2 on: May 18, 2012, 01:54:39 AM »
I tried spritename earlier and nothing happened. So I tried it again and now it magically works.   >:(
I am sometimes baffled by Unity sometimes.  :-[
Thanks for that dlewis, feels really embarrassing asking such a stupid question but hey, I have no shame. :)

dlewis

  • Guest
Re: Changing a sprite in code
« Reply #3 on: May 18, 2012, 10:45:06 PM »
Thanks for that dlewis, feels really embarrassing asking such a stupid question but hey, I have no shame. :)

Stupid questions are my speciality! :P