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. Dismiss Notice

How can I get a reference to a sprite with a string value at runtime?

Discussion in '2D' started by JimMakesGames, Jun 24, 2015.

  1. JimMakesGames

    JimMakesGames

    Joined:
    Nov 13, 2013
    Posts:
    21
    I've got a bunch of sprites in a spritesheet and I'm wondering if there's a way to get references to them at runtime by calling them by their name, like with a string, rather than having to make a reference manually in the inspector for all of them.

    So something like:
    Code (CSharp):
    1. WeaponIcon.sprite = GetSpriteByName("SwordIcon1");
    Is something like that possible? Hope that makes sense. Thanks.
     
  2. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
  3. JimMakesGames

    JimMakesGames

    Joined:
    Nov 13, 2013
    Posts:
    21
    Good idea. Thanks for the suggestion. However I'm wondering if that will result in more draw calls or some other less efficient process because of loading the sprite sheet for every item...