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?
Answer by UnityCoach · Jul 24, 2018 at 09:43 AM
I sort of understand where your trying to go there.
Here are a few things to consider.
Make a list of CharacterObject instead, you can always get the GameObject through its gameObject property.
Use a abstract base class
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();
}
Answer by TheSojourner · Jul 24, 2018 at 12:59 PM
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?
Well, this really depends of what you intend to do with that list.
I'm just saying you can always reference the game object anyway.
Your answer
Follow this Question
Related Questions
Can't add Orbital script - "The script needs to derive from MonoBehaviour!" 0 Answers
How do you make a random 2D shape in Unity? 0 Answers
How to make multiple game object work with the same scripts? 0 Answers
How To Find GameObject From List And Add To Player List 0 Answers
Unity C# how to Foreach variable in Model/Contract 0 Answers