Objects, GameObjects, Lists, and lists of GameObjects with Objects, oh my!

Usually I like to handle myself, but I think I bit more than I can chew with this one.

I’m trying to create a script, that I intend to use a lot, which should instantiate a GameObject, inside a list of GameObjects, and then it’s going to attach a script to said GameObject within the list, and then it’s going to call for the list, to get the GameObject, to send whatever variables of the script.

            public static List<GameObject> characterList = new List<GameObject>();
public static GameObject NPC;
public static void stuff()
{
    characterList.Add(Object.Instantiate(NPC));
    characterList[characterList.Count - 1].AddComponent<CharacterObject>();
    characterList[characterList.Count - 1].GetComponent<CharacterObject>().AutoSetEverything();
}

Since the game is supposed to be a management game full of little units going around doing things, I’m wondering if this would be a good way to proceed. Any advice, folk?

I sort of understand where your trying to go there.

Here are a few things to consider.

  1. Make a list of CharacterObject instead, you can always get the GameObject through its gameObject property.
  2. Use a abstract base class
  3. or use an interface

This is just food for thoughts…

public interface INonPlayable
{
    public void AutoSetEverything();
}
public class NonPlayableCharacter : Character, INonPlayable
{
    public void AutoSetEverything()
    {
    }
}

public static List< INonPlayable > characterList = new List< INonPlayable >();
public static GameObject NPC;
public static void stuff()
{
    characterList.Add(Object.Instantiate(NPC).AddComponent<NonPlayableCharacter>() as INonPlayable);
    characterList[characterList.Count - 1].AutoSetEverything();
}

Brainracking!
In my original design, I started with a List of CharacterObject, which was working all well and good!
Though reading about Unity and the way it handles the game objects, and the scripts therein contained, led me to believe that it would have been a better design to game the list of game objects instead.

My reasoning was that since scripts are components of GameObjects, then the ideal thing would have been to create instances of a template game object, that then would be modified with the scripts accordingly.

I thought it’d be too much hassle to find the way to link a perpetually moving index of a list to an ever-increasing number of NPCs that are supposed to be automatically tagged as DontDestroyOnLoad.

@UnityCoach
Would you recommend sticking to the CharacterObject list, instead of a GameObject list?