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
3
Question by pedronaroga · Dec 25, 2012 at 11:56 PM · c#classmonobehaviournamespace

How to use MonoBehaviours inside namespaces.

While reading the "What's new" section on Unity 4.0, I noticed the following:

Scripting: MonoBehaviours can now be inside namespaces.

I then went back to my Unity project, created a simple namespace as follows:

 namespace Controllers {

     class CharacterController : MonoBehaviour {

         [...]

     }

 }

When trying to attach that component to a GameObject I got an error saying my class was not named the same as my file, even though my file was CharacterController.cs. When removing the namespace definition, it worked fine.

How should I name my files to make it work with namespaces?

Thanks in advance.

Comment
Add comment · Show 4
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 MaGuSware™ · Dec 26, 2012 at 12:07 AM 0
Share

Hmm, this won't be a solution to the problem ( At least I don't think it will ), but can you try na$$anonymous$$g the file Controllers. I am wondering if its detecting the namespace as the name.

avatar image Bunny83 · Dec 26, 2012 at 12:28 AM 0
Share

I don't have a solution either since I'm still on my tablet so I can't test it and I'm still using 3.5 at the moment :)

However the first thing I would suggest is to avoid class names of built in classes in general.

Next thing I would try is rena$$anonymous$$g the file to "Controllers.CharacterController.cs".

In most cases, even in big projects, you don't really need namespaces. If you develop middleware I still would keep components in the global namespace. $$anonymous$$eep in $$anonymous$$d if you want to create a base class which is derived from $$anonymous$$onoBehaviour you can place it in a namespace without any problems as long as the concrete classes have their own file and are in the global namespace.

avatar image Jessy · Feb 08, 2013 at 07:49 PM 0
Share

That's insanity! $$anonymous$$any products on the Asset Store have ridiculous prefixes, when they ought to be in namespaces.

avatar image $$anonymous$$ · Feb 08, 2013 at 08:13 PM 0
Share

Interesting... I just tested this, and I have no such problem. Namespace seems to work fine. I think it's a useful feature and should be used, we use it in other C# stuff anyway, so why not in Unity?

Btw. when I named the class CharacterController, Unity gave a warning that it has the same name as a built-in, so watch out for that (it works regardless, you just may have conflicts later).

3 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by mtalbott · Sep 25, 2013 at 04:28 PM

I found an interesting querk. While it does work to have a MonoBehavior class inside a namespace, class methods can not use default arguments.

i.e. The following works fine:

 namespace TestNamespace {
     public class TestClass : MonoBehaviour {
         public void TestMethod(string test) {
             Debug.Log(test);
         }
     }
 }

however, the following does not work:

 namespace TestNamespace {
     public class TestClass : MonoBehaviour {
         public void TestMethod(string test = "default") {
             Debug.Log(test);
         }
     }
 }
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 GFX47 · Oct 01, 2013 at 01:14 PM 0
Share

I confirm the default arguments bug :( Did anyone file a report about it?

avatar image TrickyHandz · Oct 01, 2013 at 01:25 PM 0
Share

Default arguments in C# were not added until .Net 4.0, which is not supported by Unity.

avatar image Joskym · Oct 01, 2013 at 01:31 PM 2
Share

@TrickyHandz I'm using default arguments with Unity. Of course not in a $$anonymous$$onoBehaviour class but it works in other classes

avatar image
4

Answer by ThePunisher · Feb 08, 2013 at 08:43 PM

Could be your class is private to the namespace? Try adding public in front of the class definition.

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 Cygon4 · Apr 06, 2013 at 08:36 PM 0
Share
  • good observation and the most likely cause, I think.

I've put all my components in namespaces and I can confirm the file needs to have the name of the class without the namespace. Very convenient, too, since the Add Component - Script menu gets additional sublevels for namespaces.

avatar image
0

Answer by vexe · Mar 25, 2014 at 03:39 PM

I have faced the same issue - however the reason was very peculiar. My script was inside a namespace and it was being identified and seen alright. Then I added a named parameter - which is usually just added to refine code readability, like so:

 item = value.Init(@from: this);

Where Init:

 Item Init(Slot from) ...

I removed the named param, and it worked!

However, I was unable to replicate the problem, i.e. create a script inside a namespace and use a named param. I don't know what's special about my case.

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 Nition · May 02, 2014 at 01:02 AM 0
Share

Well, I can tell you that you're not crazy, because I had the same issue here. Removing the namespace, or removing the named parameter both fix it. Default paramteres seem to cause the same issue.

Edit: Apparently it's fixed in some unreleased future version. http://issuetracker.unity3d.com/issues/combining-namespaces-mbs-with-default-params-break

avatar image slippdouglas · Jan 22, 2019 at 01:33 AM 0
Share

Why did you type an @-sign?  `@` is a special character in C# which should only be used in the rare cases that you need a variable named the same thing as a native C# keyword.  from is not a C# keyword (in this context; it can be a keyword in LINQ expression, which you're not using here; see https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ ).

Long story short, you should write item = value.Init(from: this);.  Considering the occasional bugginess of $$anonymous$$ono, it's plausible that the @ was causing the problem, not the named param as a whole.

avatar image Nition slippdouglas · Jan 22, 2019 at 01:40 AM 0
Share

Named parameters were causing a real issue at the time (@ sign not necessary to cause it) as described in the issue tracker link I posted. It was fixed in Unity 4.5 though and as far as I know has been fine since.

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

21 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

Related Questions

Calling functions from other namespaces and scripts. 0 Answers

Can't use Namespaces with Mono and 1 class 1 namespace file 1 Answer

How do c# scripts get accessed by the Unity Engine 2 Answers

Classic Class in C#? 3 Answers

"The class named is not derived from MonoBehaviour or ScriptableObject!", although class is derived from MonoBehaviour 2 Answers


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