Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

How to load sprite on script

Discussion in '2D' started by iktakgames, Nov 14, 2013.

  1. iktakgames

    iktakgames

    Joined:
    Sep 5, 2012
    Posts:
    39
    Hi i need to acceso to an Script with Resourced.Load but this not wors, my image name is "Character" and the Sprite Name is "Chatacter_1".
    Thanks
     
  2. unitylover

    unitylover

    Joined:
    Jul 6, 2013
    Posts:
    346
    Create a reference variable and store your sprite in it, then reference the variable when you want to load it. Unless you include more information or a bit of code then I cannot help you further.
     
  3. Ravl

    Ravl

    Joined:
    May 14, 2013
    Posts:
    35
    Hi, same question I have. I will try to be more specific:

    I create a Sprite Object: "ScreenBackground"

    and I have in my Assets folder 2 images: "living.jpg" and "bedroom.jpg".

    I want to asign to my ScreenBackground objects one of those 2 images while running the game. For example when pressing 1 to show living and when pressign2 to show bedroom. How should we do that?

    I created this script and stick it to my ScreenBackground object, but I still don't know how to 'load' the image at runtime..
    Code (csharp):
    1.  
    2.     private SpriteRenderer mySprite;
    3.  
    4.     // Use this for initialization
    5.     void Start () {
    6.         mySprite = GetComponent<SpriteRenderer>();
    7.     }
    8.  
    Thanks
     
  4. Callski

    Callski

    Joined:
    Feb 3, 2013
    Posts:
    130
    I would recommend creating a prefab of the sprite and dragging that prefab into the resources folder. Then you can Instantiate the object Instantiate(Resources.Load("TestSprite")); If you wanted to you could also save the sprite as a GameObject variable to manipulate after creation: GameObject sprite = (GameObject)Instantiate(Resources.Load("TestSprite"));
     
  5. Ravl

    Ravl

    Joined:
    May 14, 2013
    Posts:
    35
    Well, I have like 80 scenes, so 80 images and in every scene i plenty of objects (images).

    You are telling me to create a prefab for every image??
     
  6. iktakgames

    iktakgames

    Joined:
    Sep 5, 2012
    Posts:
    39
    The problem is that i have the sprite and resources,but i cant load it from resoures and add to spriterenderer sprite. This not wotks.