A Quick Guide to C# with Unity by Patrick Felicia

A Quick Guide to C# with Unity by Patrick Felicia

Author:Patrick Felicia
Language: eng
Format: epub
Publisher: Patrick Felicia
Published: 2020-01-17T00:00:00+00:00


As we will see later, because most of your classes will inherit by default from the class MonoBehaviour, they will, by default, include several methods, including Start and Update that you will be able to override (i.e., modify for your own use).

Scope of variables

Whenever you create a variable in C#, you will need to be aware of the scope and access type of the variable so that you use it where its scope makes it possible for you to do so.

The scope of a variable refers to where you can use this variable in a script. In C#, we usually make the difference between global variables and local variables.

You can compare the term local and global variables to a language that is either local or global. In the first case, the local language will only be used (i.e., spoken) by the locals, whereas the global language will be used (i.e., spoken) and understood by anyone whether they are locals or part of the global community.

When you create a class definition along with member variables, these variables will be seen by any method within your class.

Global variables are variables that can be used anywhere in your script, hence the name global. These variables need to be declared at the start of the script (using the usual declaration syntax) and outside of any method; they can then be used anywhere in the script as illustrated in the next code snippet.

class MyBike

{

private int color;

private float speed;

public void accelerate()

{

speed++;

}

}

In the previous code we declare the variable speed as a member variable and access it from the method accelerate.

Local variables are declared within a method and are to be used only within this method, hence the term local, because they can only be used locally, as illustrated in the next code snippet.

public void Start()

{

int myVar;

myVar = 0;

}

public void Update()

{

int myVar2;

myVar2 = 2;

}

In the previous code, myVar is a local variable to the method Start, and can only be used within this function; myVar2 is a local variable to the method Update, and can only be used within this method.

Events

Throughout this book and in C#, you will read about and use events. So what are they?

Well, put simply, events can be compared to something that happens at a particular time, and when this event occurs, something (e.g., an action) needs to be done. If we make an analogy with daily activities: when the alarm goes off (event) we can either get-up (action) or decide to go back to sleep. When you receive an email (event), you can decide to read it (action), and then reply to the sender (another action).

In computer terms, it is quite similar, although the events that we will be dealing with will be slightly different. So, we could be waiting for the user to press a key (event) and then move the character accordingly (action), or wait until the user clicks on a button on screen (event) to load a new scene (action).

In Unity, whenever an event occurs, a function (or method)



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.