Search Unity

How do I add a custom script to a game object via its name as a string?

Discussion in 'Editor & General Support' started by Agilapathy, May 30, 2021.

  1. Agilapathy

    Agilapathy

    Joined:
    Jun 29, 2018
    Posts:
    21
    Hey all,

    I'm trying to figure out how to add a script I made (In this instance, it's named "PickaxeTool") to a game object through the name of the script as a string. This is what I tried already and it didn't work; it prints null:

    Code (CSharp):
    1. System.Type t = System.Type.GetType(typeString);
    2. print(t);
    3. itemInstanceObj.AddComponent(t);
    I've read some things online talking about how you need to specify the Assembly its getting the script from, but I don't really follow what that means. Any help is greatly appreciated.

    Cheers.