• 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 ZachIsAGardner · Jul 21, 2018 at 05:28 PM · classnamespace

Unity Can't find script class because of namespace

I have a monobehaviour script. I attempt to add it to a gamobject and I get this dialogue


Can't add script component 'MyScript' because the script class cannot be found. Make sure that there are no compile errors and that the file name and class name match.


The usual issue is that the filename and classname do not match or there are compiler errors. This is not the case. I've diagnosed the issue as being related to the namespace. For whatever reason if the class is inside a namespace unity will not be able to find it.


I've tried renaming the namespace but that did not work. It seems to be just namespaces in general. This issue seems to be with just this class as well. I just finished a big refactor of my project and other monobehaviours attached to gameobjects are working fine.


Any clue as to why this is happening? How exactly does unity find classes?


Thanks

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by ZachIsAGardner · Jul 21, 2018 at 05:52 PM

Hi I've found the solution to my issue.


The issue was that I had a static using in my class file. For whatever reason a MonoBehaviour can't be attached to a GameObject if it has a static using.


I was trying to reference an enum from a static class. To shorten the syntax I just put a static using at the top. But that's a no no apparently.


So instead of this at the top:

 using static YourNamespace.Constants;


You can do this whenever you want to talk about your enum:

 YourNamespace.Constants.YourEnum


I'll leave this up in case someone else runs into this if das coo

Comment
Add comment · 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
0

Answer by JonPQ · Jun 05, 2019 at 05:44 PM

I've had the same problem but with no static. but did have many classes in the same file. Possibly unity cannot find the correct class, maybe associated with being inside namespaces.

solution was to separate mono classes out into separate files. so there is only one class that definitely matches the filename.... no ambiguities.

Note:- once broken... it stayed broken until deleting the project files in the root (above assets/) and the library folder, and re-importing everything.

Comment
Add comment · Show 2 · 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 Bunny83 · Jun 05, 2019 at 07:35 PM 0
Share

Having multiple $$anonymous$$onoBehaviours within a single file is not really related to any namespace issue. $$anonymous$$onoBehaviours that should actually be attached to gameobjects always need to be in a seperate file since its classname has to match the file name, no matter if it's in a specific namespace or in the global namespace.

avatar image JonPQ Bunny83 · Jun 07, 2019 at 06:59 PM 0
Share

Thanks for the explanation. makes sense. Splitting the file with many classes, into a file for each class worked for me. I had seen many other comments in other threads about namespace related similar issues.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

89 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Calling functions from other namespaces and scripts. 0 Answers

How to implement custom using directives? 1 Answer

What is the default namespace 0 Answers

The type or namespace name 'StateMachine' could not be found (are you missing a using directive or an assembly reference?) 1 Answer

Can't access another script, think it may be due to namespace/public class ? 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