Hey. I have a prefab with Monster class and a list of monsters. I want to fill this list with instances of this prefab. How do I do that? Monster.Instantiate will create instances of monsters in the scene, I just want to have this list to access at a later time. I’ve read I shouldn’t create MonoBehaviour instances of class via new Class() constructor. I could just set location of these prefabs to place them somewhere far away from game scene, but it doesn’t seem to be good solution.
1 Answer
1make prefabs out of other monsters too and add them to your list via inspector , click at the monster class prefab and a list will be in the inspector drag and drop other prefabs in it or use gameObject.SetActive(false);
What if the script containing this list isn't a MonoBehaviour. It's a static class, I can't attach it to any GameObject in scene to add List elements via inspector. Also it's not what I was asking for, I want to instantiate n (variable) monsters in code, without adding them to scene. Is there a way to istantiate them and "hide" from game view?
– Afooyes you can
– 767_2gameObject.SetActive(false);will disable and hide themyou can Instantiate then use SetActive(false) to hide in scene.
– Louis_Watsonand Louis Watson just agrees with me what can be better ,right louis
– 767_2haha sorry couldn't see your post when I commented ;) other alternative would be to store the path string of the asset rather than the Prefab itself and use AssetDatabase to load it. All the info is in the documentation :)
– Louis_Watson