How do I change sprite ingame?

So basically my player is an astronaut and when he goes into the airlock I want his sprite to change from spacesuit to without space suit I have both sprites made bud how do I make the player switch between two sprites without remaking the animations? Sprite layouts are the same.

This depends on how you constructed your player. Do you have one sprite and you animate it by changing the image in the sprite renderer? Or do you have multiple game objects in the player and animate these components, e.g. body, head, legs, arms?

If you have multiple game objects in your player, you can use something similar to below.
This expects there to be a directory in Assets named Resources. Within the resources directory, you need to have your sprites without the space suit.

    public void Reskin(string newSkin) {
        Sprite[] sprites = Resources.LoadAll<Sprite> (newSkin);

        SpriteRenderer[] renderers = GetComponentsInChildren<SpriteRenderer> ();

        foreach (SpriteRenderer renderer in renderers) {
            foreach (Sprite sprite in sprites) {
                if (sprite.name == renderer.name) {
                    renderer.sprite = sprite;
                }
            }
        }
    }

I simply have one sprite with multiple sprites basically I import one image and I slice it. I think this can’t work for me.

I think the above approach might work for you. Check out this video from time 20:00

It explains how to reskin the player.

Thanks so much my friend! You saved my butt :stuck_out_tongue:

Hello all,
In this video, the character game object has no sprite renderer component. Please explain me how the animator component can work without it ?

@Yrod you don’t need to have a Sprite Renderer for the animation to work in Unity. You can animate a scale, position and rotation of a gameobject without any visual component to it.

Okay thank you ! It was a stupid question though.