I am Percipience. I really want to start with creating games but I have a very low budget… Therefor, I need to learn as much as I can so I can do as much as possible with myself, to decrease the required budget to create a masterpiece! Everyone has to start somewhere, correct? Well, I decided I wanted to start with scripting in C#. I already understand the basics of the Unity3D Engine. I am, however, stuck on one little problem… I don’t understand classes in C#!! Let me give you an example:
public class FunctionsAndVariables : MonoBehaviour
The code above is what I do not understand! Could someone please describe each part for me? Thank you!
The line of code you posted is a part of the class header and all classes will start out roughly the same way, with minor tweaks.
public is a visibility modifier that determines who can see and use that class. If this class definition is not inside another class or data structure then there is no need for it to be anything other than public else you wouldn’t be able to use it in your code anywhere else. There are a total of three visibility modifiers, the other two are private and protected.
class is the datatype/datastructure that the definition is representing. Just like if you were declaring variables you would type the datatype then the identifier: int x;
FunctionsAndVariables this is the identifier for the class. This is how the class will actually be referenced when used in code. Once this class has been defined then it can be referenced in code using this name in place of the datatype. So you are able to something like: FunctionsAndVariables fav;
: this is an operator and it is how C# derives classes from one another, this is also known as inheritance.
MonoBehaviour this is a unity provided class that handles all the functions like Start and Update, this class also allows scripts to be attached to objects. Placing this class name after the : operator tells C# to derive this class from the class MonoBehaviour. This means that anything MonoBehaviour can do, this class can now do (To an extent). MonoBehaviour will now be known as the base class for this class. This can be done with more than just the MonoBehaviour class. MonoBehaviour uses the above mentioned visibility modifiers to restrict and allow any derived classes access to the inner workings of the class. Anything marked with the protected keyword may not be seen by the public but it can be seen by any classes that derive from this base class. Anything marked by the private keyword may not be seen by the public or by any classes that derive from this class.
Below is a rough exmaple of everything I explained for you to examine:
// This is a generic class
public class ClassA {
// since this is marked private only ClassA has access to it
private int x;
// This is the default constructor
public ClassA() { }
// Since this is marked protected only ClassA and any
// class derived from ClassA will have access to it
protected void FunctionOne() { }
// Since this is marked private only ClassA has access to it
private void FunctionTwo() { }
}
// ClassB is being derived from ClassA
public class ClassB : ClassA {
// Default constructor
public ClassB() {
// This is valid. Class B has access to this function because
// it is marked protected
FunctionOne();
// This is invalid and will throw a compiler error. ClassB does not know about
// FunctionTwo because it does not have access to it because it is marked private.
FunctionTwo();
}
}