#Skills
1 messages · Page 1 of 1 (latest)
So you want to be able to add a random component from a list of skills?
@tribal nimbus
Hi there, thanks for messaging 🙂
Yea thats basically what I want to do. I know how to add components and stuff, the only thing im stuck with is to get them in a list / dictionary / whatever where I can add them to the game object after being randomly selected without instantiating them.
This is an experiment, just from me searching around and what I’ve seen on the discord.
Add a list of skills: public List<Skill> skills;
Then when you add the component
AddComponent<typeof(skill[randomIndex])>();
The problem now I think is referencing them since Monobehavior’s might need to be on an object to be referenced although I’m not sure
Let me give you a little more perspective
I'm actually working on a little "hospital" simulator for practing and dealing with (patients) illnesses instead of skills. Each illness has a chance to randomly add another illness to the patients, just like in real life.
@tribal nimbus oh and I forgot. Someone else was doing something similar a couple days ago, using strings to map to components. Might be worth to check out
Let me try real quick
Yea I guess I got it working
Thanks for your assistance. I wonder if that's the best way tho lol
Just had the time to read this
Use Component[] diseases = GetComponents(typeof(Disease));
Then AddComponent<typeof(diseases[randomIndex])>();
Get components creates an array of all components of a parent class into an array
https://docs.unity3d.com/ScriptReference/GameObject.GetComponents.html
@tribal nimbus is that kind of what you were looking for?
Well wouldn’t that only work if the scripts are actually attached to the game object to begin with?
No the gameobject spawns with like 1 to 3 diseases and each of these have a random chance of adding a new „follow up“ disease to the game object which I can now define on each illness thanks to your support. Maybe I’ll find I better way but it certainly works for now 🙂 thank you