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
4
Question by whydoidoit · Dec 10, 2012 at 07:26 PM · monobehaviourunity 4

Are namespaces supported in Unity 4?

So apparently MonoBehaviours should be able to be in a namespace in U4. However, I get a lot of warnings about the "The class defined in script file 'x' does not match the filename" - is there something I can do about this?

EDIT: Just to be clear

  • The warning only happens if the class is in a namespace

With the namespace:

alt text

Without the namespace:

alt text

The code (with the namespace):

Here's the code:

 using System;
 using UnityEngine;
 
 namespace RadicalLibrary
 {
     
     public class ActivatedObject : StateMachineBehaviour, IMessage, IAmActivated
     {
         public string message;
         public string activatedMessage;
         public string Name = "Take 001";
         public bool touchEnabled = true;
         public float Speed = 1;
         private StateMachine _sm;
         public GameObject targetObject;
         
         public bool IsActivated()
         {
             return Activated;
         }
         
         private bool _activated;
         public bool Activated {
             get {
                 return _activated;
             }
             set {
                 _activated = value;
                 var go = targetObject == null ? gameObject : targetObject;
                 if (!String.IsNullOrEmpty (Name)) {
                     if (value) {
                         go.animation [Name].speed = Speed;
                         go.animation.Play (Name);
                     } else {
                         if (go.animation [Name].time == 0) {
                             go.animation [Name].time = go.animation [Name].length;
                         }
                         go.animation [Name].speed = -Speed;
                         go.animation.Play (Name);
                     }
                 }
             }
             
         }
         
         void TouchActivated()
         {
             Activated = !Activated;
         }
         
         
         #region IMessage implementation
         public string GetMessage ()
         {
             return Activated ? activatedMessage : message;
         }
         #endregion
     
         
     }
     
     public class WaitForRelease : State
     {
             
         private Action _action;
     
         public WaitForRelease (Action actionOnRelease = null)
         {
             _action = actionOnRelease;
             
         }
             
         public override void OnUpdate ()
         {
             if (TouchInterface.TouchCount == 0) {
                 SetState (StateMachine.Inactive);
                 if (_action != null) {
                     _action ();
                 }
             }
         }
             
     }
     
 }
screen shot 2012-12-11 at 09.02.30.jpg (408.5 kB)
screen shot 2012-12-11 at 09.03.17.jpg (408.8 kB)
Comment
Add comment · Show 7
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 whydoidoit · Dec 11, 2012 at 09:25 AM 0
Share

Ugh I've totally messed this question up trying to fiddle with it, apologies!!! :S

So YES there are two classes in that file (I can't actually read my own indentation, WaitForRelease is supposed to be a child class of ActivatedObject).

  • If there are two classes in a file then it works fine without a namespace

  • If there are two classes in a file then putting them both in a namespace fails

  • If I put WaitForRelease as a child of ActivatedObject it works fine

If the people who were answering/helping on this would like to post answers again - I can tick and upvote where necessary! Thanks!

avatar image whydoidoit · Dec 11, 2012 at 09:29 AM 0
Share

Argh, I'm still getting comment notifications on an answer which (for me at least) is invisible! @doug__ if you can "see" your answer can you make sure that it is undeleted? Otherwise could you post it again.

avatar image whydoidoit · Dec 11, 2012 at 09:38 AM 0
Share

@Shrandis & @$$anonymous$$arkFinn - I've also lost your comments - I'm having a bad morning in all respects!

avatar image Shrandis · Dec 11, 2012 at 09:45 AM 0
Share

I haven't said much other than making a pun about this interesting behavior and asking if it was possible that you are getting a warning because you have 2 classes in a single file (which means one of them obviously doesn't match the file name)

So, reading your new comment, is the conclusion "we cannot have 2 same-level classes in a namespace in the same file" ?

avatar image whydoidoit · Dec 11, 2012 at 09:46 AM 0
Share

Yes that is it exactly... 2 classes are fine with no namespace, but broken when there is one.

Show more comments

5 Replies

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

Answer by Philipp · Jan 08, 2013 at 02:57 PM

Here's one more that took us a long time to figure out: MonoBehaviours inside namespaces break, if they have a method with a default parameter (such as public void Foo(bool bar=true)).

Comment
Add comment · Show 7 · 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 KyleStaves · Jan 08, 2013 at 03:44 PM 0
Share

Oh man, thanks for that! This one was killing me to the point where I gave up on namespaces for my monobehaviour derived classes and made sure they all had a company-specific name prefix to avoid collisions with middleware.

avatar image JeanSimonet · Aug 26, 2013 at 02:43 AM 0
Share

Holy crap, this is totally true. Thanks for the tip!

avatar image mikeschuld · Oct 01, 2013 at 04:31 AM 2
Share

This is the same issue I found myself. I have posted this as a bug to the Unity $$anonymous$$m.

avatar image nventimiglia · Nov 12, 2013 at 11:55 PM 0
Share

Thank you. Just ran into this myself.

avatar image lightfoot · Dec 12, 2013 at 12:04 PM 0
Share

Amazing. Just changed a bunch of stuff and got this error -- never thought to check the default parameter. Crazy. Thanks for spotting this!

Show more comments
avatar image
4

Answer by KyleStaves · Jan 04, 2013 at 04:54 PM

I just wanted to add that anything can break it; I had an enum in one of mine driving me crazy trying to fix.

One more thing, [ExecuteInEditMode] breaks the class as well - I wouldn't be surprised if other, similar flags break things.

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 whydoidoit · Jan 05, 2013 at 05:21 AM 1
Share

Good points - and also I noticed that ScriptableObject behaviour in namespaces is dubious - can't quite work out what the rules are yet - but I had to move some out (but not all).

avatar image JeanSimonet · Oct 01, 2013 at 12:48 PM 1
Share

Also, I noticed that the object picker doesn't work with ScriptableObject in namespaces (the object selection window you get when you use EditorGUI.ObjectField() for instance)

avatar image whydoidoit · Oct 01, 2013 at 01:07 PM 0
Share

Very annoying that, having to write my own right now...

avatar image
2

Answer by whydoidoit · Dec 11, 2012 at 09:53 AM

Here's the answer:

  • If there are two classes in a file then it works fine without a namespace

  • If there are two classes in a file then putting them both in a namespace fails

  • If, in the example, I put WaitForRelease as a child of ActivatedObject it works fine, so nested classes work

  • If the class has methods with default parameters it fails [Added due to answer below]

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 willrmiller · Dec 17, 2012 at 08:21 PM 0
Share

I too am having this problem. Some $$anonymous$$onoBehaviour-derived classes inside of namespaces seem to work fine, while others do not.

avatar image whydoidoit · Dec 17, 2012 at 08:29 PM 0
Share

Have you made sure that there is only one class definition of any kind inside the file with the namespaced $$anonymous$$onoBehaviour - that fixed it for me...

avatar image mikeschuld · Oct 01, 2013 at 04:32 AM 0
Share

@whydoidoit: can you update the answer of this to say it is the one below. The default parameter is the real culprit in this case.

avatar image
1

Answer by vexe · May 18, 2014 at 10:42 PM

So for me it wasn't just default params, but also named params! (i.e. namespaces AND named params - if you use either one alone you shouldn't have problems)

This will fail:

 namespace MyNamespace
 {
    public class Settings : ScriptableObject
    {
         public static void IntMethod(int x) { }
         public static void Test()
         {
             IntMethod(@x: 10); // <-- Named param
         }
    }
 }

 // Somewhere else...
 Debug.Log(ScriptableObject.CreateInstance<Settings>()); // prints "()"


This will succeed:

 namespace MyNamespace
 {
    public class Settings : ScriptableObject
    {
         public static void IntMethod(int x) { }
         public static void Test()
         {
             IntMethod(10); // <-- Pass arg normally
         }
    }
 }

 // Somewhere else...
 Debug.Log(ScriptableObject.CreateInstance<Settings>()); // prints a valid value
 

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
1

Answer by SonicBloomEric · Jul 13, 2015 at 01:40 PM

What Works (4.5.5+)

I came across this issue in testing with versions of Unity between 4.5.5 and 5.1.1. It seems that the following are no longer limitations (in at least 4.5.5+):

  • Two classes in the same file, both within a namespace.

  • Having a method with a default parameter within the MonoBehaviour/ ScriptableObject subclass that's in a namespace.

I have not tested with named parameters and therefore cannot speak to them.

What Doesn't Work (as late as 5.1.1)

However, I did run afoul of an issue with namespaces and MonoBehaviours/ScriptableObjects. The following WILL NOT WORK:

  • A non-MonoBehaviour/ScriptableObject class that has a method with a default parameter appears before the MonoBehaviour/ScriptableObject subclass in the script file.

The following examples are expected to be in a file called TestClass.cs.

Works: No Namespaces

The SupportClass is simply a wrapper for an int. This works.

 using UnityEngine;
 
 [System.Serializable]
 public class SupportClass
 {
     public int anInt;
 
     public void SetTheInt(int i = 1)
     {
         anInt = i;
     }
 }
 
 public class TestClass : MonoBehaviour
 {
     public SupportClass someVal;
 
     public int TestMethod(int i = 1)
     {
         return i;
     }
 }
 

Doesn't Work: Add a Namespace

Both classes are within the namespace.

 using UnityEngine;
 
 namespace TestNamespace
 {
     [System.Serializable]
     public class SupportClass
     {
         public int anInt;
 
         public void SetTheInt(int i = 1)
         {
             anInt = i;
         }
     }
 
     public class TestClass : MonoBehaviour
     {
         public SupportClass someVal;
 
         public int TestMethod(int i = 1)
         {
             return i;
         }
     }
 }

Works: Reorder Classes Within the Namespace

Simply changing the order of the classes causes this to work again.

 using UnityEngine;
 
 namespace TestNamespace
 {
     public class TestClass : MonoBehaviour
     {
         public SupportClass someVal;
         
         public int TestMethod(int i = 1)
         {
             return i;
         }
     }
 
     [System.Serializable]
     public class SupportClass
     {
         public int anInt;
 
         public void SetTheInt(int i = 1)
         {
             anInt = i;
         }
     }
 }

Works: Remove the Default Parameter from the Support Class

Simply removing the default parameter from the support class when it appears first, also fixes the problem.

 using UnityEngine;
 
 namespace TestNamespace
 {
     [System.Serializable]
     public class SupportClass
     {
         public int anInt;
 
         public void SetTheInt(int i)
         {
             anInt = i;
         }
     }
 
     public class TestClass : MonoBehaviour
     {
         public SupportClass someVal;
 
         public int TestMethod(int i = 1)
         {
             return i;
         }
     }

Final Note

You can have multiple support classes defined before the MonoBehaviour/ScriptableObject subclass. This will work 100% fine until you add a default parameter to any method in any support class that appears in the script file before the MonoBehaviour/ScriptableObject subclass. In fact, if you have multiple support classes, you can put those without default parameters in their methods before the MonoBehaviour/ScriptableObject subclass and those with them after. This also works fine.

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 Mikael-H · Sep 28, 2016 at 04:29 PM 1
Share

Wow a very detailed answer! I was a bit unsure of what works and doesn't with namespaces these days, thanks for clearing it up for me!

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

25 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

Related Questions

XML Serialization problem 0 Answers

Adding a script to a prefab 1 Answer

Get Monobehaviour play mode instance from editor instance 0 Answers

Leaves, Branches disappear in viewport on Tree Creator Object 1 Answer

Does the Shuriken Particle System in Unity 4 not allow particles to render prior to collision? 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