Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
6
Question by MonkeyAssassin8 · Jul 22, 2011 at 03:27 PM · scriptingbasicsmonobehaviourusing

Can someone explain 'Using ..." and "MonoBehaviour" in C#

I have been using Javascript for a while now and want to move on to C#, but I can't get my head around the code at the start of a script.

I would like to know what the 'Using' (eg: Using UnityEngine, Using System.Collections.Generic) lines do and if they are required for certain things, and also what the 'MonoBehaviour' does.

Thanks in advance :)

I was also wondering if you can create two or more classes? eg, one to refer to another script and another for MonoBehaviour in the same script.

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

7 Replies

· Add your reply
  • Sort: 
avatar image
9

Answer by Peter G · Jul 22, 2011 at 04:34 PM

using has a number of uses in C#. There are two that go at the top of a file that you are asking about and a third that you don't use too often in Unity.

  1. using _; imports namespace. Namespaces are a collection of classes ands other data types that are used to categorize the library. UnityEngine for example is a collection of all the classes related to Unity. System.Collections is all the classes in .Net related to holding groups of data such as hashtable and array list. It is required whenever you use a class in that namespace. For example if you want to write to files then you need "System.IO."

  2. Another use is that you can use it to disambiguate between different classes or to shorten a lengthy type declaration. This functions similar to a macro that you can use it to replace text.

       using System;
          using UnityEngine;
         //This is a common use for this to.  Both UnityEngine and System have a class called object so you should specify which one "Object" refers to.
         using Object = UnityEngine.Object;
     
          //BE CAREFUL DOING THIS BECAUSE IT CAN MAKE YOUR CODE HARD TO READ
          // BUT IT IS OCCASIONALLY USEFUL.
          using MyDictionary = System.Collections.Generic.Dictionary<int, string>;
    
    

The : MonoBehaviour tells Unity what class your class is derived from. Inheritance is a fundamental part of object-oriented programming (OOP) because it gives you several special features. It let's objects share common functionality, and it allows you to treat descendants like the base class. This behavior is called polymorphism and the wiki page explains it fairly well. You don't need to explicitly declare classes in javascript, but that's a common practice in C#.

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 J3-Gaming · Jul 22, 2011 at 04:40 PM 0
Share

I learnt something new today, I didn't know you could do: using Object = UnityEngine.Object;

That's awesome, way better than always declaring the namespace when there is ambiguity

avatar image
2

Answer by J3-Gaming · Jul 22, 2011 at 04:11 PM

The using statement will allow you to "use" (duh) other classes in your code.

If you didn't declare Using UnityEngine you would have to write: UnityEngine.Debug.Log("Hello World");

It allows you to exclude typing the namespace all the time.

Same rules apply for the System namespace.

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 MonkeyAssassin8 · Jul 22, 2011 at 04:15 PM 0
Share

oh, I see! Thanks for your help :)

avatar image
2

Answer by tnetennba · Jul 22, 2011 at 04:19 PM

There are actually two different ways to use using in C#

This is the less used way, the using statement. http://msdn.microsoft.com/en-us/library/yh598w02(v=vs.80).aspx

The way I suspect that you are using it is the using directive: http://msdn.microsoft.com/en-us/library/sf0df423(v=vs.80).aspx

The monobehaviour is the class that your script inherits from. http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.html

You will not have seen that before if you had been using Javascript as all javascript scripts automatically inherit a monobehaviour.

I would suggest that yo read up on inheritance in object orientated programming if you are not sure. http://www.csharp-station.com/Tutorials/lesson08.aspx

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 AVividLight · Jul 22, 2011 at 03:37 PM

Hey MonkeyAssassin8,

I have been using C# for a little while, but I am certainly not a guru... Anyway, you almost never have to change the line "using UnityEngine;"; all that does it tell the Mono Engine that you are going to be programming in Unitys C#. Then next line "using System.Collections;"... Well, I am not quite sure myself, but if I had to guess I would say it might be the style that you are going to write in... Also, it might contain various information about what Syntax you can you...

-Hope I Helped!


-Edit-

I looked it up on MSDN, and they give the following explanation "The System.Collections namespace contains interfaces and classes that define various collections of objects, such as lists, queues, bit arrays, hash tables and dictionaries."


-Edit 2-

Okay, let me try to explain again...

Line1: "using UnityEngine;" Basically, this tells MonoDevelop that you are using Unity's version of C#. Notice you can reference GameObjects and CharterControllers?

Line2: "using System.Collections;" As I understand it, this tells MonoDevelop that you will be programming using things like arrays, hash tables, and queues. There are other things you can put there, such as System.Collections.Concurrent, System.Collections.Generic, System.Collections.ObjectModel, and System.Collections.Specialized. If you are just starting out, I don't suggest you change it, as System.Collections is very similar to Unity's Java Script. Also, you don't need to add anything extra to your code if your using System.Collections.

Line3: "public class ClassName : MonoBehaviour {" The first part "public" tells the code that this script can be accessed by other scripts. The next part "class" is telling the script that the next line will be the title of your code. "ClassName" this can be changed to what ever you want, but make sure you change the script's name to the same thing. Now "MonoBehaviour"... this is the base class every script derives from.

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 MonkeyAssassin8 · Jul 22, 2011 at 03:47 PM 0
Share

Thanks for the quick response, but I still don't understand. you said the line "using UnityEngine" tells unity what to highlight. Is it needed or does it just highlight words. same with "using System.Collections"; do I need it to create a list or hash table, etc? And what about "$$anonymous$$onoBehaviour"?

avatar image Bunny83 · Jul 22, 2011 at 07:28 PM 1
Share

There is not really a "Unity's version" of C#. It's plain C#. You don't even need to put the using statement there (like $$anonymous$$ightyGoob already said) but everytime you want to use a class from the UnityEngine assembly you need to write the full classname including the namespace.

 public class $$anonymous$$yScript : UnityEngine.$$anonymous$$onoBehaviour
 {
     public UnityEngine.Transform prefab;
     [...]
 }
avatar image AVividLight · Jul 24, 2011 at 03:14 PM 0
Share

Why is my Answer down-voted?

avatar image Peter G · Jul 24, 2011 at 03:50 PM 0
Share

I didn't vote you down, but my guess is that its because of this line:

"using UnityEngine;" Basically, this tells $$anonymous$$onoDevelop that you are using Unity's version of C#.

you aren't using Unity's version of C#, you are using the standard C#. That lines means you are importing the UnityEngine namespace. It tells the compiler that you are referencing objects in that Namespace so it knows where to find the objects definition.

avatar image AVividLight · Jul 25, 2011 at 05:39 PM 0
Share

But, in essence, you are using a Unity Specific version of C#... If you put that at the top of a normal C# code, it won't work.

avatar image
0

Answer by robertgalp · Jun 27, 2014 at 08:30 AM

More details about ...C# using statement

robert

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
  • 1
  • 2
  • ›

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

9 People are following this question.

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

Related Questions

using UnityEditor outside of Editor folder when compiling build? 4 Answers

Can some one help me with splash screen? 1 Answer

What is the difference between "prefix-casting" (Type)variable - and "as-casting" (variable as Type)? 2 Answers

using System, Monobehaviour, Camera, GameObject and most other thing don't work anymore/unrecognized/don't exist in current context 0 Answers

What are the Syntax Differences in C# and Javascript? 7 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