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
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.
Deep Learning with Python by François Chollet(12570)
Hello! Python by Anthony Briggs(9914)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9796)
The Mikado Method by Ola Ellnestam Daniel Brolund(9777)
Dependency Injection in .NET by Mark Seemann(9337)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8296)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7070)
Microservices with Go by Alexander Shuiskov(6831)
Practical Design Patterns for Java Developers by Miroslav Wengner(6750)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6693)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Angular Projects - Third Edition by Aristeidis Bampakos(6097)
The Art of Crafting User Stories by The Art of Crafting User Stories(5622)
NetSuite for Consultants - Second Edition by Peter Ries(5560)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5367)
Kotlin in Action by Dmitry Jemerov(5062)
