Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning C# by Developing Games with Unity 2021

You're reading from   Learning C# by Developing Games with Unity 2021 Kickstart your C# programming and Unity journey by building 3D games from scratch

Arrow left icon
Product type Paperback
Published in Oct 2021
Publisher Packt
ISBN-13 9781801813945
Length 428 pages
Edition 6th Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Harrison Ferrone Harrison Ferrone
Author Profile Icon Harrison Ferrone
Harrison Ferrone
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Preface 1. Getting to Know Your Environment 2. The Building Blocks of Programming FREE CHAPTER 3. Diving into Variables, Types, and Methods 4. Control Flow and Collection Types 5. Working with Classes, Structs, and OOP 6. Getting Your Hands Dirty with Unity 7. Movement, Camera Controls, and Collisions 8. Scripting Game Mechanics 9. Basic AI and Enemy Behavior 10. Revisiting Types, Methods, and Classes 11. Introducing Stacks, Queues, and HashSets 12. Saving, Loading, and Serializing Data 13. Exploring Generics, Delegates, and Beyond 14. The Journey Continues 15. Pop Quiz Answers
16. Other Books You May Enjoy
17. Index

Introducing classes

We've seen how variables store information and how methods perform actions, but our programming toolkit is still somewhat limited. We need a way of creating a sort of super container, containing variables and methods that can be referenced from within the container itself. Enter classes:

  • Conceptually, a class holds related information, actions, and behaviors inside a single container. They can even communicate with each other.
  • Technically, classes are data structures. They can contain variables, methods, and other programmatic information, all of which can be referenced when an object of the class is created.
  • Practically, a class is a blueprint. It sets out the rules and regulations for any object (called an instance) created using the class blueprint.

You've probably realized that classes surround us not only in Unity but in the real world as well. Next, we'll take a look at the most common Unity class and how classes function in the wild.

You can find an in-depth guide to classes in the Microsoft C# documentation at https://docs.microsoft.com/en-us/dotnet/csharp/fundamentals/types/classes.

A common Unity class

Before you wonder what a class looks like in C#, you should know that you've been working with a class this whole chapter. By default, every script created in Unity is a class, which you can see from the class keyword on line 5:

public class LearningCurve: MonoBehaviour

MonoBehaviour just means that this class can be attached to a GameObject in the Unity scene.

Classes can exist on their own, which we'll see when we create standalone classes in Chapter 5, Working with Classes, Structs, and OOP.

The terms script and class are sometimes used interchangeably in Unity resources. For consistency, I'll be referring to C# files as scripts if they're attached to GameObjects and as classes if they are standalone.

Classes are blueprints

For our last example, let's think about a local post office. It's a separate, self-contained environment that has properties, such as a physical address (a variable), and the ability to execute actions, such as sending out your mail (methods).

This makes a post office a great example of a potential class that we can outline in the following block of pseudocode:

public class PostOffice
{
    // Variables
    public string address = "1234 Letter Opener Dr."
    // Methods
    DeliverMail() {}
    SendMail() {}
}

The main takeaway here is that when information and behaviors follow a predefined blueprint, complex actions and inter-class communication become possible. For instance, if we had another class that wanted to send a letter through our PostOffice class, it wouldn't have to wonder where to go to fire this action. It could simply call the SendMail function from the PostOffice class, as follows:

PostOffice().SendMail()

Alternatively, you could use it to look up the address of the post office so you know where to post your letters:

PostOffice().address

If you're wondering about the use of periods (called dot notation) between words, we'll be diving into that in the next section—hold tight.

Communication among classes

Up until now, we've described classes and, by extension, Unity components as separate standalone entities; in reality, they are deeply intertwined. You'd be hard-pressed to create any kind of meaningful software application without invoking some kind of interaction or communication between classes.

If you remember the post office example from earlier, the example code made use of periods (or dots) to reference classes, variables, and methods. If you think of classes as directories of information, then dot notation is the indexing tool:

PostOffice().Address

Any variables, methods, or other data types within a class can be accessed with dot notation. This applies to nested, or subclass, information as well, but we'll tackle all those subjects when we get to Chapter 5, Working with Classes, Structs, and OOP.

Dot notation is also what drives communication between classes. Whenever a class needs information about another class or wants to execute one of its methods, dot notation is used:

PostOffice().DeliverMail()

Dot notation is sometimes referred to as the . operator, so don't be thrown off if you see it mentioned this way in documentation.

If dot notation doesn't quite click with you yet, don't worry, it will. It's the bloodstream of the entire programming body, carrying information and context wherever it's needed.

Now that you know a little more about classes, let's talk about the tool you'll use the most in your programming career—comments!

You have been reading a chapter from
Learning C# by Developing Games with Unity 2021 - Sixth Edition
Published in: Oct 2021
Publisher: Packt
ISBN-13: 9781801813945
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image