Search Unity

  1. Unity 2019.2 is now released.
    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:
    38
    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:
    345
    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:
    26
    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:
    26
    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:
    38
    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.