Sprite Renderer not working

Hello Dear Unity Community,

i am trying to display an inventory inside my game. For that i need to display a sprite after pressing G.
So i tried rendering the sprite as the following code shows, but nothing seems to be working…
Am i doing something wrong?

public class Inventory : MonoBehaviour
{
    public Sprite slot;

    void Start()
    {
        GameObject inventory = new GameObject("Test");
        SpriteRenderer renderer = inventory.AddComponent<SpriteRenderer>();
        renderer.sprite = slot;
    }
}

Thx for the A2A <3

Can you be more specific on what is not working? Have you added some debug.logs to inspect each aspect a little more?

I just tried it, your code works perfectly.

I would suggest setting your Game Objects Sprite Renderer order in layer property to +1 higher than the container’s.

Seems like the code works so I am just assuming it is an order in layer issue. You must be rendering behind your inventory. Now you also haven’t quite released the information of whether you’re using UI for the inventory. I am just assuming that you are. If that’s the case, you want your spites to be UI elements and you would increase the sorting order element +1.