Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by burnumd · Jun 23, 2010 at 08:59 PM · c#addcomponentgenerics

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mike 3 · Jun 24, 2010 at 03:45 PM 0
Share

Creepy monodevelop! (I actually went and checked it was rena$$anonymous$$g my files correctly when you said that)

avatar image Tetrad · Jun 24, 2010 at 06:44 PM 0
Share

Be sure to mark your answer as accepted so the Community bot doesn't bump this question as "unanswered"

avatar image burnumd · Jun 24, 2010 at 07:02 PM 1
Share

I plan to, but I evidently have to wait 25 hours after answering to mark my own question as answered.

avatar image
2

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.

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mike 3 · Jun 23, 2010 at 09:17 PM 0
Share
  • for being much more plausible than $$anonymous$$e :D

avatar image burnumd · Jun 24, 2010 at 03:07 PM 0
Share

Thanks for the suggestion. No, they're in their own files, DatabaseConnectionWithWWW.cs and DatabaseConnectionWithHttpWebRequest.cs respectively.

avatar image Tetrad · Jun 24, 2010 at 03:13 PM 0
Share

What happens if you replace your generic GetComponent call with one that uses typeof? i.e. GetComponent( typeof( DatabaseConnectionWithHttpWebRequest ) ) as DatabaseConnectionWithHttpWebRequest;

avatar image burnumd · Jun 24, 2010 at 03:41 PM 0
Share

Nah, it eventually worked without me having to do any work. Well, any code-changing work at least. See my answer.

avatar image burnumd · Jun 24, 2010 at 05:22 PM 0
Share
  • because your mentioning of the class and file name match requirement led me to actually look at the filenames in Unity/Finder. :) Thanks!

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image burnumd · Jun 24, 2010 at 03:08 PM 0
Share

Sadly, no, they're not DLL's, just C# files in the Asset directory.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges