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 /
  • Help Room /
avatar image
4
Question by Jack-Mariani · Oct 03, 2014 at 06:14 PM · c#scriptingbasicsnamespace

When to use a namespace?

This is a noob question.

When I code I usually use class from MonoBehaviour. Now reading others code I see that sometimes they use namespace.

When is it better to use namespace? And how I use them?

Moreover, not sure if it's a related question: what's the difference between MonoBehaviour and namespace?

Thank you, as always.

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
15
Best Answer

Answer by Landern · Oct 03, 2014 at 06:26 PM

When I code I usually use class from MonoBehaviour. Now reading others code I see that sometimes they use namespace.

MonoBehaviour is a class in namespace UnityEngine. When you're reading others code, a namespace definition is not a class definition.

Example:

 namespace SomeGame.Player
 {
   public class Hero : MonoBehavior
     {
         // ...
     }
 }

When is it better to use namespace? And how I use them?

Use a namespace to ensure your scoping of classes/enum/interface/etc won't conflict with existing ones from other namespaces or the global namespace. Namespaces scope classes, this is important due to many times similar naming conventions are used for class names. This can confuse the compiler and exceptions are thrown. You can explicitly without importing or using the namespace create/invoke in method.

The Unity namespace documentation explains this pretty well.

Moreover, not sure if it's a related question: what's the difference between MonoBehaviour and namespace?

MonoBehaviour is a class in namespace UnityEngine. When you're reading others code, a namespace definition is not a class definition.

MonoBehaviour is a class that other classes derive from, it exists in the UnityEngine namespace and it derives from Behaviour, Behaviour derives from Component and Component derives from UnityEngine.Object.

When a class/type is instantiated, it is constructed in such a way that it begins from the base class up. Depending on how access specifiers are used, you may have access to methods, properties, indexers, fields, etc from all the deriving classes.

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 Owen-Reynolds · Oct 03, 2014 at 07:06 PM

Namespaces are in C# for people who already know what they are, and like them. Since you don't, probably best to never use them.

C# is a real programming language, used for lots of things. It has many, many, many extra features, which no one person will ever use all at once. Experienced programmers get in arguments about the correct way to set things up, the best commands, and even features that should never be used.

Namespaces are mostly used by people with traditional computer science backgrounds. Most basic Unity scripts, written by regular game programmers, aren't going to bother with them.

A lot of times, you find a script that you can tweak to do what you want, but you have to figure it out first. If it uses namespaces, it probably also uses a few other tricky things you haven't learned yet, and you aren't going to be able to figure it out enough to use it.

Comment
Add comment · Show 18 · 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 ByteSheep · Oct 03, 2014 at 07:32 PM 5
Share

Haha that's a rather depressing answer ;)
To elaborate: I think one of the most common situations where you might want to use a namespace is when you are going to share your code with other people and you want to avoid na$$anonymous$$g collisions with existing scripts in their projects.
I guess you could look at namespaces being a bit like a library of code.

A collection of classes that can be imported to other scripts when needed; with the syntax:

 using NameSpace.Whatever; // import a collection of useful classes

I guess it's a good thing namespaces exist, but you probably won't have to worry too much about them unless you plan on creating a c# library for the asset store and you are worried about potential duplicate class names.
Hope that's correct info.

avatar image Jack-Mariani · Oct 04, 2014 at 07:19 AM 0
Share

I've computer science background :). But it is old background and I want to refresh it. And now that I've a few answers I think I understand the basics of namespace.

And, btw, maybe I'll not use them, but since I'm working with others code, I need to understand them.

They don't seem that complex now.

So thank you for your answer :).

avatar image Owen-Reynolds · Oct 04, 2014 at 04:55 PM 0
Share

In practice, Unity people often make a class with all public static functions and variables, which functions pretty much the same as a namespace. Esp. since C# uses the dot for both (in A.B you can't tell if A is a class, or an instance of a class.)

Then, you often want to use the Inspector to set a few variables, or start a coroutine, which means you're using the $$anonymous$$onobehaivior class with the script on an empty.

avatar image zhuchun · Mar 23, 2016 at 10:29 AM 4
Share

I'd like to down vote this, sorry. The fact is that namespace is EXTRE$$anonymous$$ELY important! If everybody don't use it, I can guarantee that nobody can write a game for production. Why? Think about it, nowadays almost EVERY game developer use Assets, you may import lots of scripts with same names like "PlayerController", "Scene$$anonymous$$anager" etc. Namespace is the ONLY way to protect you from infinite rena$$anonymous$$g. It IS very simple by the way.

 //In class A
 namespace $$anonymous$$yAwesomeNameSpace{
 Class A{
 //Whatever
 }
 }
 
 //In other scripts.
 using $$anonymous$$yAwesomeNameSpace;

avatar image Crowbeak · Jun 14, 2016 at 06:25 AM 2
Share

I can't downvote this because I don't have enough reputation here, but I would if I could because it is not a useful answer. "Oh, if you don't know what they are, don't worry about it! They're for people who know more than you," is basically what you're saying here, which defies the point of having an answers forum in the first place. On top of not actually answering the question, it's patronizing.

avatar image Owen-Reynolds Crowbeak · Jun 14, 2016 at 01:59 PM 0
Share

I'm going to put this comment here to try and help you remember to downvote when you are able.

I didn't even say the worse thing about this Q: it's just a general C# question, and doesn't belong in a Unity area. It's not the askers fault, but the answerers. $$anonymous$$uch, much, much better explanations of every aspect of C# are in sites like stackoverflow. Everyone who doesn't tell you this is trying to keep down the competition by making sure you never learn to code.

It's not patronizing. If a "noob" (OP's words) wants to know when to use a namespace, the best answer is "don't -- it's an advanced thing you don't need now."

avatar image Crowbeak Owen-Reynolds · Jun 15, 2016 at 04:12 AM 4
Share

If you're going to reject questions like this as a matter of policy, fine. If not, answer it or ignore it. A wishy-washy half-answer isn't going to help anyone. That said, I think this has gone off-topic for long and far enough and will not be responding to this anymore.

Show more comments
Show more comments

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

8 People are following this question.

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

Related Questions

Access script from a namespace 1 Answer

C# Beginner Question -- How to call a function from a separate script in multiple other scripts 2 Answers

GetAxis help? 1 Answer

Play animation on mouse click 1 Answer

Need help in translating javascript code into c#. 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