Hello. I am in the process of creating an RPG game. One thing that I am trying to do is have it so that, when you enter a town for the first time, a bunch of random “villagers” are generated. I was wondering if there was a way to instantiate a “villager” prefab and have randomized stats applied to it. I understand this may be a very broad question, but I am having a very hard time figuring out how this would work. For example, when the scene is loaded, several villagers are generated. These villagers would have stats like health, race, class, etc. I would like any answers in c# please. Anything would really help!
As you point out it’s a very broad question, but I’ll try and touch on a few bits that should get you started.
First off, you mention when a scene loads. You can do this in two ways, one is to simply place a component in that scene and then use the Awake() or Start() method of the component to respond to the loading of the scene, Alternatively if this is not the first/main scene in your game you could have an object that is not destroyed when you load a second scene, and have that script register for the SceneManager.sceneLoaded event which gets called when a scene has loaded.
As for the random townsfolk, I would first define a script that contains the stats for “A villager” As for generating random villages, You would just use a loop to instantiate a prefab with that script on it, for the randomness part of it you just have to apply a good old fashioned application of the Random class be it Random.Range or Random.value.
Your commented script looks fine for assigning stats. Bring that in on an empty gameobject and then use if statements to figureout if you want to load an ElvenFemale or a HumanMale or whatever. Then use Resource.Load() to bring in the model you want.
In order to save stuff you can look up filestream there are lots of easy examples on how to save ints and bools as long as you’re not getting too complicated. Give each village a number, and name the save file that. When you look for the save file if it exists you load up they villagers already saved. If it doesn’t exist you know you need to create the village from scratch.
In other words study Resources.Load() and filestream