- Home /
How to create multiple Characters?
Hello,
I want to create multiple characters programmatically in a script. The Problem is, I can't find anything where or how do I store the Assets of this Character. Lets say, we have a script which is holding some items of the player. Another script is holding data about the player stats. But a player have also a model in the scene. My first idea was, to create the model and add the two scripts as the components. Then I have to store my characters in the script what is creating the characters as game objects. The problem is, that I only can access the components with the GetComponent() method. So I have to store the components as well or I have to call this Method a lot! My second idea was, to create a GameObject with the creation script und in this script I store the Characters in a List. Each character have a string with its model and own instances of items and stats.
What is the right way? Unity let me do quit everything.
Thanks a lot!
I hope I'm not saying something you already know but you actually don't have to call GetComponent() in a lot of cases, though it's perfectly fine to use it. An alternative is if you make a variable public or put [SerializeField] next to it in a script, you get a box in the inspector on the script component that you can plop an instance onto to reference pretty much anything, including classes.
It kind of sounds like you are on the right track. I don't think there is a specific right way. The wrong way would be if your implementation didn't work at all or was impossible to refine, expand or build upon later. This is what we often do in prototype phase is just throw something together to get a working example and then fix it up later when we have a better idea of our needs.
Answer by Koyemsi · May 05, 2018 at 03:31 PM
I think I would organize the project this way :
Characters are prefabs with their own scripts.
Put a level manager in the scene (an empty GO + script). The script would centralize all infos about the level, like the number of enemies / characters, and maybe even the player's info if it's more convenient for you . It would spawn characters (instantiation), and give them the desired parameters (health, ammo and so on). A reference to the spawned characters should be kept in a List or Array for convenience.
Does this answer your question ?