0

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.

  • 1
    You could use an `enum` ... you can't simply typecast a `string` into a `Component` ;) I guess it would help a bit to see your different types you want to be able to assign. I guess [this](https://stackoverflow.com/a/11107562/7111561) might help – derHugo Mar 24 '20 at 06:49
  • @derHugo You can turn a string into a component (see my answer), but I agree with you an `enum` may be the better way to go here, as it better guards against mispellings. – Philipp Lenssen Mar 24 '20 at 10:57
  • @PhilippLenssen this is not the same as OP trying to directly typecast a `string` to `Component` .. I know you can get the `type` on other ways .. also more reliably as your answer .. see link I added to my comment ;) – derHugo Mar 24 '20 at 15:33

1 Answers1

0

To turn a string into a component, use

string componentName = "FooBar";
gameObject.AddComponent(System.Type.GetType(componentName));

It's not very error-safe, though. Having an enum to wrap this, as derHugo suggests, might be more future proof as it guards against misspellings for new inspector assignments.

Philipp Lenssen
  • 8,818
  • 13
  • 56
  • 77
  • This answer basically would make it a duplicate of [Convert String to Type in C#](https://stackoverflow.com/questions/11107536/convert-string-to-type-in-c-sharp/11107562#11107562) and similar questions. Also there you can see more reliable ways of getting a `Type` via its name. Your answer might fail if the wanted `Type` and your code are in different Assemblies (which would be true for any Unity built-in component) – derHugo Mar 24 '20 at 15:35
  • @derHugo what about converting an object to a component? – Zachary Atkinson Games Mar 24 '20 at 19:34
  • @ZacharyAtkinsonGames well that would be only possible if this `object` actually **is** a `Component` reference .. you can't just cast any `object` to `Component` .. e.g. what should `0.5f as Component` result in? ;) – derHugo Mar 25 '20 at 06:17