Understand Variable.
Objective: Understand Variable.
Before doing this ,download Unity and Install It then download Visual Studio Community which both of them are free. I wont show step to do this just search online you can see a tutorial for this.
In the first step create C# script ,to do this go to the unity editor <Project <Assets ,right click and choose C# Script then rename it. All code are written inside the class a ,class represent a behavior of an object eg. player ,enemy etc.
When Create a C# script ,unity will highlights the file name in order for you to rename the file as you want ,butt this may prompt some error if you click away once unity give you this option then you come rename ,make sure the file name and class name match other wise the code won’t run.
A variable represent a storage location ,each variable has a type which determine a value which can be stored in. Just like in real world ,we put clothes in closet or we put tea in tea bottle also variable behave similar to this.
To open our script double click our script ,
Syntax Of a Variable.
Variable have 3 required keyword and forth is an option the we end with semi colon.
- Access Modifier this can be public or private ,when we set an access modifier to public this mean we can access this variable out side of this. If its private we can only access this variable inside this class.
- A type of a variable
- A name of Variable
- Option assign its value.
- This need to be add when we choose to end in number 3 or after we assign the value. This means here its where our code end or our statement end.
Common Type of Variables.
There are common four type of variables an int ,float ,string and a bool
- an Int is whole number variable ,you can not assign a float to an it. To Assign variable is to give a value to a variable eg. public int myAge = 16;
- A float in number with decimal point ,normally in C#(Csharp) we need to add f prefix in the end of its value. Eg public float speed = 10f;
- String this is the collection of character ,We normally use double quote after equal sign then we add our character in the middle of double quote eg. public float string = “Suleiman”;
4. When define bool it set default value to false ,a bool has two value true or false.eg public bool hasReached = false, or public bool hasReached = true.
Best Practice for Variable
We write a variable using camel Case, we start with small letter in the first word and Start with capital Letter in every next word.
But before writing a name Microsoft put standard to write private variable ,with underscore .Eg public int _myAge = 16; , its not mandatory but most of developers follow this standard.
When writing bool name we write it as question ,hasKey ,isDead etc.
This is the End of this Article see you in the next one.