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.
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);
}
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?
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 !
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)
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:
…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…
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)”