Add a custom component by searching for it's name

Hello,

I have created a small modding system and I want players to be able to add custom components to a certain object. I already know how to check which object to modify and everything but I have a small problem, this modding system uses strings. And AddComponent only works with Scripts. I could limit mods to have a certain name for their scripts, but what if they want multiple scripts. That’s why I need the AddComponent to use the one with the name said in the string.

My question is: is there a way to AddComponent()

Note: I’m doing it in C#.

I hope I was clear enough, if you don’t understand something, ask me and I’ll try to explain it better.

Thank you!

    void Start () {
        //We have a string holding a script name
        String ScriptName = "YourScriptNameWithoutExtension";
        //We need to fetch the Type
        System.Type MyScriptType = System.Type.GetType (ScriptName + ",Assembly-CSharp");
        //Now that we have the Type we can use it to Add Component
        gameObject.AddComponent (MyScriptType);
    }
1 Like

Thank you!
But how would I do it so that it would loads a script from a certain path?
Because I want it to be custom, I need to add the Application.dataPath, should I add it right here?

string path = Application.dataPath;
System.Type MyScriptType = System.Type.GetType (path + ScriptName + ",Assembly-CSharp");

EDIT:
I tried your code and it doesn’t work, I get this error:
AddComponent asking for invalid type
UnityEngine.GameObject:AddComponent(Type)

gameObject.AddComponent();

Oh, you actually want the user to throw C# scripts at Unity at runtime, that unity doesn’t know about?

I have never tried that.

You would need to create or load assemblies at runtime.

http://docs.unity3d.com/Manual/scriptsinassetbundles.html

Didn’t even notice that. Oh yeah, that’s a whole other bag of cats now. I have no experience with anything like that. I might take a look at those links myself. Good luck @PandawanFr !

Thanks! I kinda want players to be able to add custom scripts that are not referenced in Unity and reference them at runtime…

I have searched all around google, and don’t really get how I could do it.

Reflection plus asset bundles.

Asset bundles let you create assemblies that Unity games can load at runtime.

Reflection lets you examine assemblies at runtime.

Be warned, this is a deep rabbit hole to start down.

1 Like

What a wonderful use of simile haha

Lol. Totally accidental.:sweat_smile:

I thought asset bundles could not contain scripts…

http://docs.unity3d.com/Manual/scriptsinassetbundles.html

I didn’t say it was going to be easy, just possible.

Ok, well thing is I already have a big system so I just want to know if there is a way to do it without assets bundles or anything of the sort. I am using a txt file for the mod itself, and using a “Parser” which will get each lines and separate them into words. And then I test for the right word and do some stuff. I wanted it to go like that “cube addComponent NAMEOFCOMPONENT.”

So if you know a way to do it without redoing everything, then you can tell me, if you don’t know, then I’ll just try to make a super advanced system (maybe so advanced that it will become it’s own programming language)

Implementing a run time language is not that uncommon. LUA is a popular one I see bandied about. Never tried it myself.

It’s still not clear whether the components you’re talking about (NAMEOFCOMPONENT above) are scripts provided by the mod, or scripts already in your game, and the mod merely adds them.

In the former case, yeah, it’s a deep rabbit hole indeed. You might be better off embedding Lua (which is fairly easy).

In the latter case, it’s not so bad. In Unity 4 and earlier, you could just call AddComponent with the name of the component. Unity 5 has deprecated that, and it changed my code to this:

  CityPartpart = UnityEngineInternal.APIUpdaterRuntimeServices.AddComponent(noob, "Assets/Scripts/CityParts/CityPart.cs (33,19)", className) asCityPart;

…which is ugly and generates a warning in the editor, but it works. It’s on my to-do list to find a cleaner method… I suspect that @Hikiko66 's technique above is the way to do it.

Ok, well, I’ll see later for custom scripts, but do you know a way for script that already exist?

Also, Joe, what type should the variable be?

EDIT: I did that: modifiedObj.AddComponent(param[3]); and Unity requested an API update for this script, so I let it update and I’l see if this works:
UnityEngineInternal.APIUpdaterRuntimeServices.AddComponent(modifiedObj, “Assets/Files/Script/Mods/ModParser.cs (151,5)”, param[3]);

EDIT 2: Ok that works, now let’s see if it works with custom scripts…

YUUSS! It works, Unity tells me to update it too but it works so I’m not going to.

Thanks everyone!

1 Like

Ok, now I just want to know if there is a way to do the same but for other type of resources? If not then that’s fine. I just wanted to know.

Other type of resources? Like what?

Like Textures… Prefabs…

EDIT: I found out that because the code you gave me is obsolete (even though it works), I can’t compile/build the game. I have to remove/comment the line.
I thought @Hikiko66 's technique would work, but I get a weird error/warning message.
“AddComponent asking for invalid type
UnityEngine.GameObject:AddComponent(Type)”