How to ignore missing components in list unity 2020?


How to ignore these missing elements?
or
How to remove my destroyed target from the list? Source

public class SkinMananger : MonoBehaviour {

public GameObject[] characters;
public int selectedCharacter = 0;
public GameObject carrot;

public void NextOption()
{
   characters[selectedCharacter].SetActive(false);
   selectedCharacter = (selectedCharacter + 1) % characters.Length;
   characters[selectedCharacter].SetActive(true);


}

public void BackOption()
{
   characters[selectedCharacter].SetActive(false);
   selectedCharacter--;
   if(selectedCharacter < 0)
   {
       selectedCharacter += characters.Length;
   }
   characters[selectedCharacter].SetActive(true);
}

public void PlayGame()
{

   PlayerPrefs.GetInt("selectedCharacter", selectedCharacter);
}

}

and I use Destroy(gameObject); for my characters when certain actions take effect. So I need a skin manager to work also when they are destroyed. As at this moment, it stuck.

Presumably you’re hitting null reference exceptions when calling SetActive because the indexed element has been deleted?

Check that characters[selectedCharacter] != null before calling SetActive.

If you want to actually remove an element you should use List<t>.
For example, public List<GameObject> characters = new List<GameObject>();
You can call characters.RemoveAt(index) or characters.Remove(object)