Adding to List - getting error

Hi, I’m trying to create a list of sprites. I can create the list no problem but get an error saying I’m using an undefined object when I try to add to it.

Any ideas? here’s my code:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class flipBG : MonoBehaviour {

	public Sprite BG1;  // I added sprites in the inspector
	public Sprite BG2;
	public Sprite BG3;
	List <Sprite> BGs;

    void Start()
    {
        fillList();
    }       
	
    void fillList()
    {
       BGs.Add (BG1);  // error occurs here
	   BGs.Add (BG2);
       BGs.Add (BG3);
    }
}

Your variables BG1, BG2 and BG3 are of type Sprite and your list is of type GameObject which is why you are getting this error.

If you want to store sprites from these three variables then your list should be of type Sprite. Also you should initialize your list.

So your line with list becomes:

List<Sprite> BGs = List<Sprite>();