This article was made for ACM Studio’s Workshop Series.
Programming was meant to be relatively logical. Each concept in a programming language should roughly map to a solution to a problem somebody was trying to solve. C# is no different.
Here’s a unity component
public class MyComponent : MonoBehaviour {
int health;
void Start() {
health = 100;
}
}
Let’s go over the syntax.
A class
specifies that you are creating a type of thing. Kinda like a “concept” or a “class”. This class will later be used to create a new object (instance) with all the features of your class.
For example, a class might be “stuffed animal”, and it has properties like weight, height, fluffiness, color, animal type, etc. The actual instance of the stuffed animal is a particular stuffed animal. The concept of “stuffed animal” doesn’t exist, it just helps C# know what you’re talking about.
public
means that if you write other classes, they can “see” this class. That is, if you do not write public
, C# will throw an error if you try to access MyComponent
from another component.
: MonoBehaviour
defines the superclass. In the stuffed animal analogy, a stuffed animal might be a subclass of toy. In that case, we would write class StuffedAnimal : Toy
. This means that MyComponent
shares its features (member variables and methods) with MonoBehaviour
. This gives us useful functions like getting the attached GameObject (gameObject
), setting the position (transform.position
), which C# would not know about if we did not specify MonoBehaviour
.
int health
specifies a “property” (member variable/field) on the class. This is like the weight or color of the stuffed animal. Each stuffed animal has a different property.
You can set a default value for this field by doing int health = 100;
Learning to program is easier if you’re willing to make mistakes and if you’re curious about the details of what you’re doing. Whenever you read something or have a question, try it out on your machine or on an online tool like .NET Fiddle. Learn to read the error messages you see when you mess up. If the C# creators did a good job, it should usually tell you exactly how to fix simple errors. Otherwise, learn to Google your problem well.
Programming’s end goal is to manipulate data. Even for a complicated thing like a game engine, the goal is to manipulate all the objects in the game into formats that your graphics card knows how to put on the screen.
C# knows how to represent only a few types of data. It can represent an integer 0
or a decimal 0.0
. It can represent a character 'c'
or text (a string) "hello"
. You can write any of these values in C#.
You can play around with these by using some sort of “print” function (I’m sure you’ve heard of print("hello world!")
). In C#, the default is System.Console.WriteLine(whatever)
(though later, in Unity, you will be using UnityEngine.Debug.Log
).
System.Console.WriteLine(10);
System.Console.WriteLine("Hello");
This is valid code, and writing code like this is often useful. However, it is often useful to be able to use a value more than once, or to modify it over time. Both of these can be achieved using variables (which don’t necessarily need to vary). In C#, the syntax for this is
// <type> variableName = value; (btw this is a comment, it does nothing)
int integer = 10;
double dec = 11.0;
char ch = 'a';
string text = "hi!";
int integer2; // i can't use this until i set its value
integer2 = 13; // i can use it now
var integer3 = 10; // C# will infer that var = int
// var integer2; but it cannot here, since it doesn't know its value
System.Console.WriteLine(dec);
System.Console.WriteLine(dec * 2);
You can manipulate values using basic math operations, and then you can replace the current value using the equals sign =
again.
int a = 10;
int b = 40;
b = a * b / (a + b);
System.Console.WriteLine(b); // what will this be?
Objects are more complex data structures that can contain multiple pieces of data (member variables/field) alongside code (member functions). string
is actually an object
. It contains multiple char
s and provides code that lets you find the length of the string, convert it to uppercase, and all other cool things.
You can access these member variables and functions using a period:
var text = "hello";
System.Console.WriteLine(text.Length); // what will this be?
text = text.ToUpper();
System.Console.WriteLine(text); // what will this be?
An object is a value. But for C# to know that, for example, string
contains a member Length
, you must declare a class.
class Player {
public string name;
public string noise;
}
Computers allow different “precisions” of decimals (aka floating-point numbers). Most of the time, you will either use a float or a double, with double taking up twice as much space as a float. However, in C#, when you write 10.5
, that is a double. If you try to do
float x = 1.0;
it will give you an error “Literal of type double cannot be implicitly converted to type 'float’”.
To fix this, use the float suffix f
for decimals.
float x = 1.0f;