changing the SpriteRenderer in C# problems

hi there! i had an idea to create a tower defense game. i wanted create the map automaticly. i tried getting a spritesheet and make the map with it (something like 20 x 20 sprites) . but i cant figure out why i cant edit the sprites on a SpriteRenderer with my script. does anyone know how to fix this?

    public Sprite[] customSprites;

    void Awake()
    {
        customSprites = Resources.LoadAll<Sprite>("grass");
    }

    void Start()
    {
        GameObject nodes = new GameObject();
        nodes.AddComponent<SpriteRenderer>();
        nodes.SpriteRenderer.Sprite = customSprites[1];
    }

thanks alot!
kelvin-w

sorry my english isnt my first language!

this is my spritesheet where i want to load the sprites from

GameObject does not have a member called “SpriteRenderer” and the sprite field in SpriteRenderer is in lowercase. You could do this instead:

public Sprite[] customSprites;

void Awake()
{
    customSprites = Resources.LoadAll<Sprite>("grass");
}

void Start()
{
    GameObject nodes = new GameObject();
    SpriteRenderer sr = nodes.AddComponent<SpriteRenderer>();
    sr.sprite = customSprites[1];
}

AddComponent returns the component you just created.

it gave an error code

Assets/V2/Scripts/SpriteRenderer.cs(20,12): error CS1061: Type SpriteRenderer' does not contain a definition for sprite’ and no extension method sprite' of type SpriteRenderer’ could be found. Are you missing an assembly reference?

It compiles fine for me.

But it seems you have created your own SpriteRenderer class. You should use the one provided by Unity.

okay. i got it to work. there was another “thing” in my project list called “SpriteRenderer” i renamed that and it worked. im so sorry for this