Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
1
Question by Ray-Pendergraph · Jun 17, 2011 at 02:17 PM · errornamespace

A script by that name already exists... so what?

I noticed at some point this message started popping up (I think after 3.0).

 A script named 'MyClass.cs' already exists at {some other directory/namespace/MyClass.cs}. Please rename one of the scripts to a unique name.

Why? I understand what the error means ultimately, I am just not sure I understand the rationale behind the additional restriction. It's not a C# "CSxxxx" message so I assume this is a restriction that Unity has placed on top of Mono for some reason. Of course, C# allows this as long as the classes are in different namespaces.

Edit for clarification: It is true that classes extending MonoBehaviours cannot have the same name because of the fact that they are required to be in the default namespace and are thus not allowed to. This is noted in the documentation. That is presumably caused by some problem they have not worked around in the engine I suppose. The classes that I am speaking of are just plain classes and do not have these requirements.

Edit 10/8/11 - After talking with the Unity engineers it seems that this was a bit of overstepping on the code that checks script names that are MonoBehaviours. MonoBehaviours cannot be in a namespace and have to be in a file that's the same name as the class so this makes sense for them but not for other packaged (not Monobehaviour) classes. So I guess at this time it's a feature.

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

5 Replies

· Add your reply
  • Sort: 
avatar image
6

Answer by TobiasW · Jun 17, 2011 at 02:25 PM

"Unity doesn't support placing your scripts inside of a namespace at the moment. This requirement will be removed in a future version."

Source: Unity Documentation, "5. Don't use namespaces"

So: Namespaces don't work at the moment, and they want to fix that later on. I don't think any user has a definite answer to why it is that way, but it sure explains why you cannot have multiple classes with the same name.

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 Ray-Pendergraph · Jun 17, 2011 at 04:13 PM 0
Share

That's true for $$anonymous$$onoBehaviours but not for general classes that do not extend $$anonymous$$onoBehaviour. You can actually have multiple classes with the same name as long as they are in different namespaces, you just can't name a C# file in one directory the same as one in another directory.

avatar image TobiasW · Jun 17, 2011 at 06:39 PM 0
Share

Oh, okay. Then sounds like it is just a bug or a limitation based on or similar to the behavoir I wrote about. I don't think the Unity developers check here regularly; the best way might be to submit it as a bug and see what happens.

avatar image Joshua · Jun 17, 2011 at 06:42 PM 0
Share

[edit] never $$anonymous$$d :)

avatar image TobiasW · Jun 17, 2011 at 07:36 PM 0
Share

This applies to the using of namespaces, Joshua. It might not be the reason non-$$anonymous$$onoBehavoirs cannot share a name.

avatar image Joshua · Jun 17, 2011 at 11:22 PM 0
Share

You're right, apologies.

avatar image
-1

Answer by rock-in-rio · Jun 17, 2011 at 07:30 PM

You have to rename it .

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 terdos · Jun 17, 2011 at 02:30 PM

Unity does not use namespaces for one, or rather it has everything within a single namespace. I seem to remember reading that somewhere a while back. Also because Unity imposes that all monoBehaviour scripts to have the same filename as the monoBehaviour class this further restricts filenames. I believe some of this is to remain compatible with "javascript" because they don't seem to use classes for monoBehaviour, therefore the filename is the name of the class.

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 Ray-Pendergraph · Jun 17, 2011 at 04:08 PM 0
Share

Unity does not support namespaces for $$anonymous$$onoBehaviours (for some reason). Plain old C# classes can be. We have over a thousand classes that currently rest in their own namespaces... no problem. You can use namespaces, and in fact UnityEngine and UnityEditor are namespaces. The issue where $$anonymous$$onoBehaviours cannot be was a self correcting problem because the all have to be in the default namespace and therefore will not compile no matter what the name of the file is.

avatar image terdos · Jun 17, 2011 at 05:30 PM 0
Share

Ah ic, thanks for the correction :)

avatar image
0

Answer by garkin2 · Jul 16, 2012 at 03:57 AM

Its sad thing. I see no particular reasons for that behavior.

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 garkin · Dec 08, 2012 at 04:07 AM

Update. Unity 4 now supports it. Logic wins, finally. Hurray. :(

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 garkin · Dec 18, 2012 at 11:24 AM 0
Share

But its implementation still bugged, it wont work if $$anonymous$$onoBehavior children placed inside namespace and has calls with named parameters inside (and some other extended language features make it fail too). But u can override it if you will split class in 2 files, like:

Behaviour.cs { namespace ns1 { public partial class Behaviour{ / empty / } } }

Behaviour.body.cs { namespace ns1 { public partial class Behaviour: $$anonymous$$onoBehaviour { / any code u realy need must be here / } } }

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

7 People are following this question.

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

Related Questions

Error in my runtime. Namespace error 0 Answers

System.Threading.Tasks 1' does not exist in the namespace 'System.Threading.Tasks' 1 Answer

Project structure, finding scripts vis GetComponent and related errors 1 Answer

Error: 'WiiU' does not exist in the namespace `UnityEngine` 0 Answers

Moved assets out of Resources folder: the namespace already contains a definition 0 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