Script to change SpriteRenderers sprite field

I am creating a simple 2D door, when i stand in my trigger, i want it to open by switching texture from the open door texture to the closed one. I got it working with an animator where i had the transition between the textures occur when i entered and left the trigger area, but i wanted too see if it’s possible to achieve the same result with script only. I do not want to do it with 2 GameObjects however, i got that working too by toggling the enable on the spriteRenderer.

Is there anyone who can help me?
I’m pretty new to coding, but I have searched everywhere and not found a single solution that matches what i want.

var sprite1: Sprite;

gameObject.GetComponent(SpriteRenderer).sprite = sprite1;

The resources need to be in a folder named “Resources” in your project. PNG files in the Assets folder are not found. I was getting nothing (like you) until I made a folder called “Resources” put copies of my PNGs in then. Then it worked like great.
see: “Loading Resources at Runtime”