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

Putting the building blocks together

With the building blocks squared away, it's time to do a little Unity-specific housekeeping before wrapping up this chapter. Specifically, we need to know more about how Unity handles C# scripts attached to game objects.

For this example, we'll keep using our LearningCurve script and Main Camera GameObject.

Scripts become components

All GameObject components are scripts, whether they're written by you or the good people at Unity. The only difference is that Unity-specific components such as Transform, and their respective scripts just aren't supposed to be edited by users.

The moment a script that you have created is dropped onto a GameObject, it becomes another component of that object, which is why it appears in the Inspector panel. To Unity, it walks, talks, and acts like any other component, complete with public variables underneath the component that can be changed at any time. Even though we aren't supposed to edit the components provided by Unity, we can still access their properties and methods, making them powerful development tools.

Unity also makes some automatic readability adjustments when a script becomes a component. You might have noticed in Figures 2.3 and 2.5 that when we added LearningCurve to Main Camera, Unity displayed it as Learning Curve, with CurrentAge changing to Current Age.

We looked at how to update a variable in the Inspector panel in the Variables act as placeholders section, but it's important to touch on how this works in more detail. There are three situations in which you can modify a property value:

  • In Play Mode in the Unity Editor window
  • In Development Mode in the Unity Editor window
  • In the Visual Studio code editor

Changes made in Play Mode take effect in real time, which is great for testing and fine-tuning gameplay. However, it's important to note that any changes made while in Play Mode will be lost when you stop the game and return to Development Mode.

When you're in Development Mode, any changes that you make to the variables will be saved by Unity. This means that if you were to quit Unity and then restart it, the changes would be retained.

The changes that you make to values in the Inspector panel while in Play Mode do not modify your script, but they will override any values you had assigned in your script when in Development Mode.

Any changes made in Play Mode will always reset automatically when you stop Play Mode. If you need to undo any changes made in the Inspector panel, you can reset the script to its default (sometimes called initial) values. Click on the three vertical dots icon to the right of any component, and then select Reset, as shown in the following screenshot:

Figure 2.10: Script reset option in the Inspector

This should give you some peace of mind—if your variables get out of hand, there's always the hard reset.

A helping hand from MonoBehaviour

Since C# scripts are classes, how does Unity know to make some scripts components and not others? The short answer is that LearningCurve (and any script created in Unity) inherits from MonoBehaviour (a default class provided by Unity). This tells Unity that the C# class can be transformed into a component.

The topic of class inheritance is a bit advanced for this point of your programming journey; think of it as the MonoBehaviour class lending a few of its variables and methods to LearningCurve. Chapter 5, Working with Classes, Struct, and OOP, will cover class inheritance in practical detail.

The Start() and Update() methods that we've used belong to MonoBehaviour, which Unity runs automatically on any script attached to a GameObject. The Start() method runs once when the scene starts playing, while the Update() method runs once per frame (depending on the frame rate of your machine).

Now that your familiarity with Unity's documentation has gotten a nice bump, I've put together a short optional challenge for you to tackle!

Hero's trial – MonoBehaviour in the Scripting API

Now it's time for you to get comfortable using the Unity documentation on your own, and what better way than to look up some of the common MonoBehaviour methods:

  • Try searching for the Start() and Update() methods in the Scripting API to gain a better understanding of what they do in Unity, and when
  • If you're feeling brave, go the extra step and have a look at the MonoBehaviour class in the manual for a more detailed explanation
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