Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

C# Set SpriteRenderer Sprite and "sub-sprite"

Discussion in '2D' started by jospor, Nov 15, 2013.

  1. jospor

    jospor

    Joined:
    Oct 23, 2013
    Posts:
    10
    If I have a Sprite which I have sliced so that it has 256 different sprites in it and I want to set the SpriteRenderer to use for example "sprites_42", how would I do that?
     
  2. Lukas H

    Lukas H

    Joined:
    Jan 16, 2009
    Posts:
    394
    If its not meant for runtime, just drag the sprite from the project view into SpriteRenderer field.

    For runtime you could create a public sprite[] allSprites; and do a SpriteRenderer.sprite = allSprites[someIndex];
     
  3. jospor

    jospor

    Joined:
    Oct 23, 2013
    Posts:
    10
    It's meant for runtime. The thing is that the Sprite object holds all the sprites but I do not know how to access different ones through the script, all I get when loading the Sprite using Resources.Load is the first Sprite, which would be "sprites_0".