Search Unity

Sprite and SpriteRenderer

Discussion in '2D' started by PerntNo, Nov 19, 2013.

  1. PerntNo

    PerntNo

    Joined:
    Nov 13, 2013
    Posts:
    8
    I'm lost.
    Basically I would like to create a gameobject and assign a sprite to it. The problem now seems to exchange the sprite during runtime with

    void OnMouseDown

    It would have been simple to just exchange the components SpriteRenderer Sprite but I'm not able to do that. For simplicity I also created a new image and sliced it because I thought I can simply change the [] index of the slices. But I could not find a way.

    I found this one which looked promising:

    Code (csharp):
    1.  
    2.     private Sprite mySprite;
    3.  
    4.     // Use this for initialization
    5.     void Start () {
    6.  
    7.         GetComponent<SpriteRenderer>().sprite = mySprite = new Sprite();
    8.  
    But every try to do something with mySprite failed.
     
  2. PerntNo

    PerntNo

    Joined:
    Nov 13, 2013
    Posts:
    8
    I did some more tests and if I change this:

    Code (csharp):
    1.  
    2.     private Sprite mySprite;
    3.  
    4.  
    5.  
    6.     // Use this for initialization
    7.  
    8.     void Start () {
    9.  
    10.  
    11.  
    12.         GetComponent<SpriteRenderer>().sprite = mySprite = new Sprite();
    13.  
    to

    Code (csharp):
    1.  
    2.     public Sprite mySprite;
    3.  
    4.  
    5.  
    6.     // Use this for initialization
    7.  
    8.     void Start () {
    9.  
    10.  
    11.  
    12.         GetComponent<SpriteRenderer>().sprite = mySprite;
    13.  
    it works with the spite assets I drop in the inspector. So it''s possible. Can someone help how to to do the same from a script?

    Thanks a lot :)
     
  3. PerntNo

    PerntNo

    Joined:
    Nov 13, 2013
    Posts:
    8
    I found a solution for my case. Maybe someone can help with the solution ;)

    First I created a folder Resources and put a png in it. For example back.png.
    Now my working code:

    Code (csharp):
    1.  
    2.     public Sprite mySprite;
    3.  
    4.     // Use this for initialization
    5.  
    6.     void Start () {
    7.  
    8.     mysprite = Resources.Load("back",typeof(Sprite)) as Sprite;
    9.         GetComponent<SpriteRenderer>().sprite = mySprite;
    10.  
    11.  
    And that's it ;)
     
  4. loki70x7

    loki70x7

    Joined:
    Apr 19, 2010
    Posts:
    54
    This should work for you:

    Code (csharp):
    1. //Load the .png
    2. Sprite backSprite = Resources.Load("back", typeof(Sprite)) as Sprite;
    3.  
    4. //Create a new gameobject
    5. GameObject obj = new GameObject("Ducky-By-Code");
    6.  
    7. //Attach a SpriteRenender to the newly created gameobject
    8. SpriteRenderer rend = obj.AddComponent(typeof(SpriteRenderer)) as SpriteRenderer;
    9.  
    10. //Assign the sprite to the SpriteRenender
    11. rend.sprite = backSprite;
     
  5. PerntNo

    PerntNo

    Joined:
    Nov 13, 2013
    Posts:
    8
    Thanks a lot loki70x7 :)

    Your version is basically the same I have now up and running. I needed it for a "low" level beginner solution to create a memory/pairs game in 2D.

    Now I should find the time and recreate all bitmaps I use to have shadow ...
    A "highscore" is on my list as well.

    Because this is a gift, I had no time to create the tiles in blender so that I could make it a "real" 3d object. So far I like Unity. First projekt and it runs. Even with some little sound :)
     
  6. amjadyahya

    amjadyahya

    Joined:
    Nov 16, 2013
    Posts:
    11
    Hi all, I have a question regarding this, how can I access the sprites with multiple frames that I've already created in the editor, instead of loading them up from the resources folder?