- Home /
Why do I get a "Can't add component because class doesn't exist!" error using generic AddComponent?
I have two ways of connecting to a database and the one used is based on whether the user is behind a proxy. I want to have the connection auto-switch based on the setting, so they both inherit from a DatabaseConnection class (which inherits from MonoBehaviour). There should only ever be one thing of type DatabaseConnection active at a time, so I use the following to get the correct connection for the situation:
if (Settings.instance.useProxy)
{
connection = holder.AddComponent<DatabaseConnectionWithHttpWebRequest> ();
}
else
{
connection = holder.AddComponent<DatabaseConnectionWithWWW> ();
}
Debug.Log ("Using DatabaseConnection of Type " + connection.GetType ().ToString ());
return connection;
The problem I have is that at run-time whenever there is a proxy and it tries to add the DatabaseConnectionWithHttpWebRequest, I get the following error:
Can't add component because class 'DatabaseConnectionWithHttpWebRequest' doesn't exist!
UnityEngine.GameObject:AddComponent(String)
UnityEngine.GameObject:AddComponent(String)
UnityEngine.GameObject:AddComponent()
DatabaseConnection:get_instance()
From this forum post, it appears the error shows up when you call AddComponent with a string representing a class that doesn't exist (which it appears the generic AddComponent does internally), but the class seems to exist as far as I can tell (MonoDevelop auto-completes it and I get no compilation errors in the editor). Any ideas what might be wrong?
ETA: I should add that using the DatabaseConnectionWithHttpWebRequest on its own worked fine. That is, neither class inherited from anything other than MonoBehaviour and I renamed whichever one I wanted to use as DatabaseConnection.cs and had no problem using what is now the HttpWebRequest one.
Answer by burnumd · Jun 24, 2010 at 03:40 PM
It turns out MonoDevelop was showing a different filename than the actual file. I had renamed what is now DatabaseConnectionWithHttpWebRequest using the Rename context menu in MD, and it was reflected in the solution pane, but didn't actually change the file name. Since I wasn't manually adding the component in the Unity project, I didn't notice until I tried to manually add it in the inspector again. I ended up having to remove the file from the project, rename it in Finder and then add it back to the project, in case anyone else runs into this problem.
Creepy monodevelop! (I actually went and checked it was rena$$anonymous$$g my files correctly when you said that)
Be sure to mark your answer as accepted so the Community bot doesn't bump this question as "unanswered"
I plan to, but I evidently have to wait 25 hours after answering to mark my own question as answered.
Answer by Tetrad · Jun 23, 2010 at 09:09 PM
Shot in the dark: are DatabaseConnectionWithWWW and DatabaseConnectionWithHttpWebRequest class definitions in the same file? I think Unity will only recognize components that are named the same as the class, so having two in one file won't work.
Thanks for the suggestion. No, they're in their own files, DatabaseConnectionWithWWW.cs and DatabaseConnectionWithHttpWebRequest.cs respectively.
What happens if you replace your generic GetComponent call with one that uses typeof? i.e. GetComponent( typeof( DatabaseConnectionWithHttpWebRequest ) ) as DatabaseConnectionWithHttpWebRequest;
Nah, it eventually worked without me having to do any work. Well, any code-changing work at least. See my answer.
because your mentioning of the class and file name match requirement led me to actually look at the filenames in Unity/Finder. :) Thanks!
Answer by Mike 3 · Jun 23, 2010 at 09:09 PM
If you're trying to load a component which is inside a dll instead of a script in the editor, it's not possible.
You can, however, make the class in the dll a partial class, and then add a monobehaviour with the same name, no code inside and also partial.
This should let you load the one in the dll
Bit hacky, but works, without having to expose your code
Sadly, no, they're not DLL's, just C# files in the Asset directory.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to GetComponent from Class that uses Generics 1 Answer
Add Component Using String, Runtime Types? 3 Answers
Attach C# Script on runtime 1 Answer