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,401
    Code (csharp):
    1. var newSprite : Sprite;
    2.  
    3. function Start () {
    4.     GetComponent(SpriteRenderer).sprite = newSprite;
    5. }
    --Eric
     
  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.
     
    Purerainy likes this.
  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;
     
  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"
     
    CreeperPower618 likes this.
  11. diliupg

    diliupg

    Joined:
    Jan 23, 2018
    Posts:
    45
    Add the sprite to change and other variables in a scriptable object and you can change sprites, rigidbody, collider and anything else in any object you create at RUNTIME without cluttering up the hierarchy. :)
     
    CreeperPower618 likes this.
  12. CreeperPower618

    CreeperPower618

    Joined:
    Nov 28, 2019
    Posts:
    2
    At the MySprite section, if you pinned the script to a Object
     
  13. efoue2

    efoue2

    Joined:
    Jul 23, 2020
    Posts:
    14
    I know this is pretty much necroposting, but how in the world are you using javascript in unity?
     
    Michael4545 and jsullivan1201 like this.
  14. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Because of using an older version of Unity, and it was never Javascript, but a custom language more properly called Unityscript.

    --Eric
     
    Michael4545 and magichandj2 like this.