AddComponent fails when passing in type, works when passing in name
My code is generating a monobehavior based on user input at runtime, then creating a prefab and adding that monobehavior to it.
My issue is that because the object is autogenerated, AddComponent appears to be failing to properly load the monobehavior when I use AddComponent(Type mytype). The result of this call is a prefab with a broken prefab showing a "type mismatch" error.
If I pass the string name of the class, however, it works perfectly fine.
Unfortunately, the string-based call is marked as obsolete so while it technically compiles and runs, it throws an error in the logs which will be off-putting to my users.
So my question is either a) is there a known reason why the type-based api is failing when the string-based one isn't, and b) can I do anything about this, or at the very least suppress the error from the console.
It seems the script isn't fully working with string-based solution, either. Still see "type mismatch" in the script name, but the rest of the fields populate and an error is not shown. Odd behavior, for sure...
It works properly , are you sure you pass a class which is inheriting from Component? Provide your code (at least what it has to do with this) to help further.
Your answer
Follow this Question
Related Questions
Can't Add Script "The script needs to derive from MonoBehaviour!" 1 Answer
Why isn't my script inheriting from MonoBehaviour 0 Answers
My Monobehaviour Script doesn't get detected 0 Answers
Why does unity not see monobehaviour? 2 Answers
Should I inherit from Monobehaviour if the use of my C# script is only to hold script references? 2 Answers