Search Unity

Changing sprite during run-time?

Discussion in '2D' started by Jtec, Nov 18, 2013.

  1. Jtec

    Jtec

    Joined:
    Dec 13, 2012
    Posts:
    6
    Hey everyone,
    I'm just wondering how I go about changing the sprite at run-time, I know it's something basic but I can't for the life of me figure it out, thanks. :D
    EDIT: Also, I'm using javascript, thanks. :D
     
    Last edited: Nov 18, 2013
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,107
    Code (csharp):
    1. var newSprite : Sprite;
    2.  
    3. function Start () {
    4.     GetComponent(SpriteRenderer).sprite = newSprite;
    5. }
    --Eric
     
    velocette and ei1 like this.
  3. Sinron

    Sinron

    Joined:
    Oct 12, 2012
    Posts:
    16
    Eric5h5s response is an option or you can make sprite animations and animation states that allow you to change the sprite on the fly.
     
  4. Jtec

    Jtec

    Joined:
    Dec 13, 2012
    Posts:
    6
    Thanks Eric, that's exactly what I was looking for. :D
     
  5. Tekksin

    Tekksin

    Joined:
    Oct 20, 2013
    Posts:
    32
    This works great! But the animations aren't playing. It only cycles through the idle regardless of whether or not you run or jump. How do you get it to play all the same animations as the previous sprite?
     
  6. surbhitw

    surbhitw

    Joined:
    Jul 18, 2017
    Posts:
    2
    how can we do the same in C# ?
     
  7. jonramos

    jonramos

    Joined:
    Jul 28, 2017
    Posts:
    1
    Code (CSharp):
    1. public Sprite mySprite;
    2.  
    3. this.GetComponent<SpriteRenderer>().sprite = mySprite;
     
    surbhitw and Plains like this.
  8. velocette

    velocette

    Joined:
    Jul 4, 2018
    Posts:
    1
    T
    Thanks :)
     
  9. MadBabyBrain

    MadBabyBrain

    Joined:
    Nov 1, 2017
    Posts:
    1
    Where do I add the name of the sprite which I'm trying to load?
     
  10. Xeonbg

    Xeonbg

    Joined:
    Dec 20, 2017
    Posts:
    7
    "my sprite"