I'm trying to create a game about shooting different types of lasers and each of them has a different behaviour. When fired simultaneously, these lasers will synergize and become one, more powerful laser that uses all the behaviours of the individual lasers at once.
To accomplish this, I am instantiating an empty GameObject to represent the laser and then adding components based on the different behaviours it will have. Each of the regular, non-synergized lasers are defined by a scriptable object with a list of the behaviours they will use -- what object type the list is made out of is a problem I'll get to in a second. The point is, my goal is to instantiate an empty GameObject and add components from a combined list of all the behaviours I need to use.
So the problem is, I can't figure out how to add components to the object so that I can manually choose which scripts to add in the inspector window for the scriptable objects. I've tried using strings as a type, but I couldn't get that to work; the same deal came up when I tried Objects; I can't directly assign the scripts for Components; and I can't assign MonoBehaviours or get them to work in adding the component either. If possible, I'd like to use strings to add components based on the script's name, so I'll just put part of my failed code for that particular case here.
NOTES: "Laser" in this case refers to a struct I created with almost the exact same variables as in the scriptable object, so you can just treat it like it is the scriptable object, it's basically the same thing; the actual scriptable object is referred to as "LaserProperties". One of the constructors is literally just to take in a LaserProperties value and input all the relevant variables. "laserBehaviours" refers to the list of strings I am trying to turn into components. The code is just being executed in the Start function for testing purposes.
public LaserProperties props;
private void Start(){
Laser laser = new Laser(props);
GameObject laserObject = Instantiate(new GameObject("New Laser"), transform.position, Quaternion.identity);
foreach(string laserBehaviour in laser.laserBehaviours){
Component behaviour = laserBehaviour as Component;
laserObject.AddComponent(behaviour.GetType());
}
}
I am aware that I am not correctly casting a string to a component, but if anyone has any suggestions on how to do them that would be most welcoming.
If you have an answer, thank you so much this is super helpful.