How to give an image to a sprite in c# code?

I am making a game with where the main character shoots a gun. I have made it so each time the player presses “E” it creates a new bullet, a collider as a trigger (to check if it hit a target), some code to tell it how to move and a sprite renderer but when I can’t figure out how to give the sprite renderer a picture. I tried using file paths but, I have no clue what I am doing. My code looks like:

string bulletPath = "../Assets/Fighting/Bullet.png";

if (Input.GetKeyDown(“e”))


            GameObject Bullet = new GameObject("Bullet");

            BoxCollider2D hit = Bullet.AddComponent<BoxCollider2D>();



            Bullet bullet = Bullet.GetComponent<Bullet>() ;

            SpriteRenderer renderer = Bullet.GetComponent<SpriteRenderer>();

            renderer.sprite = Resources.Load<Sprite>(bulletPath);

            hit.isTrigger = true;


If you have any suggestions they would be greatly appreciated

Instead of having to generate all of that every time you press “E”, I think you should just make a “prefab”. You create the bullet in the heirarchy, collider and picture and all. Then you drag that thing back to your assets to create a prefab. This “prefab” is what you make appear every time you press “e”.

And then, you “instantiate” the prefab to make it appear. Read the “firing a rocket launcher” part.