- Home /
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.
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.
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."
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#.
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
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.
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
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.
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"?
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;
[...]
}
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.
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.