Hello guys welcome to my first tutorial in Unity Game Engine
using C# for scripting. Basic Scripting Unity C# - Part 1 Debugging.
There is list that what i will do:
- Getting know the modifier (common programming language)
- Declaring (Creating) variables of types,
- Initializing (instantiate, assign or whatever you called) a value,
- Log Debugging (printing the value of variables).
I will share what i know. I just make it into my words so if
there is a miss a thing about the code or what i say. You can just comment in below with
a respectful. We should respect each other. Okay, back to the topic.
1. Getting know the modifier
1.
Declaring variables of types
There
are many types in any language included C#. I just give some types which is primitive.
private char character;
private string characters;
private int integer;
private float floatSingle;
private double floatDouble;
private string characters;
private int integer;
private float floatSingle;
private double floatDouble;
There are some types. Which are char,
string, int, float, double.
2.
Initializing a value
So
in Unity there are many methods that have been created by Unity itself which
are
void Start
();
void Update
();
void OnDestroy
();
void OnTriggerEnter
();
void OnTriggerStay
();
void OnTriggerExit ();
void OnTriggerExit ();
There are still many of them.
So there is 2 way to initialize the variables
2. Declaring and Initializing
2. Declaring and Initializing
It means that you
declare it and initialize it directly.
//Declare
& Initialize
private char character = 'C';
private string characters "Coretan Kas";
private int integer = 20;
private float floatSingle = 11.071997f;
private double floatDouble = 11.07199705012018d;
private string characters "Coretan Kas";
private int integer = 20;
private float floatSingle = 11.071997f;
private double floatDouble = 11.07199705012018d;
3. Declaring and Initializing
It means that you declare it then initialize it undirectly. You just initialize it in methods. Suppose you declare it in world space of program, and declare it inside of start method.
//Declare
private char character = 'C';
private string characters "Coretan Kas";
private int integer = 20;
private float floatSingle = 11.071997f;
private double floatDouble = 11.07199705012018d;
private void Start () {
private string characters "Coretan Kas";
private int integer = 20;
private float floatSingle = 11.071997f;
private double floatDouble = 11.07199705012018d;
private void Start () {
//Initialize
character = 'C';
characters = "Coretan Kas";
integer = 20;
floatSingle = 11.071997f;
floatDouble = 11.07199705012018d;
}
characters = "Coretan Kas";
integer = 20;
floatSingle = 11.071997f;
floatDouble = 11.07199705012018d;
}
With these methods of
initializing a value of variables you can adjust it with your needs. Not every
type can be write in Declaring & Initializing, there are not primitive
types like GameObject, Transform will be discussed in next tutorials.
4. Log Debugging
Sometimes you need to debug your program to know is your code works or not. If you want to know the output you can write a Debug.Log then you can called which variables you want to know the value itself.
//Declare
private char character = ‘C’;
private string characters “Coretan Kas”;
private int integer = 20;
private float floatSingle = 11.071997f;
private double floatDouble = = 11.07199705012018d;
private string characters “Coretan Kas”;
private int integer = 20;
private float floatSingle = 11.071997f;
private double floatDouble = = 11.07199705012018d;
private void Start () {
//Initialize
character = ‘C’;
characters = “Coretan Kas”;
integer = 20;
floatSingle = 11.071997f;
floatDouble = 11.07199705012018d;
characters = “Coretan Kas”;
integer = 20;
floatSingle = 11.071997f;
floatDouble = 11.07199705012018d;
//Debugging – Log
Debug.Log (“character: “ + character);
Debug.Log (“characters: “ + characters);
Debug.Log (“integer: “ + integer);
Debug.Log (“floatSingle: “ + floatSingle);
Debug.Log (“floatDouble: “ + floatDouble);
}
Debug.Log (“characters: “ + characters);
Debug.Log (“integer: “ + integer);
Debug.Log (“floatSingle: “ + floatSingle);
Debug.Log (“floatDouble: “ + floatDouble);
}
Here is
the output of the program
Figure 2. Output
Program