Understand Variable.

Suleiman Abdullah
4 min readMay 23, 2024

--

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.

You will never see fish put in here
you will never see bread put here.

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.

  1. 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.
  2. A type of a variable
  3. A name of Variable
  4. Option assign its value.
  5. 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.
variable with private modifier.
Ignore some code inside the class, will talk about it in later articles. Or at the end of this one

Common Type of Variables.

There are common four type of variables an int ,float ,string and a bool

  1. 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;
  2. 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;
  3. 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.

--

--

Suleiman Abdullah

Self taught Unity Developer, who is passion about making games ,and he loves Math For Game Development