Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Learning C# by Developing Games with Unity 2020

You're reading from   Learning C# by Developing Games with Unity 2020 An enjoyable and intuitive approach to getting started with C# programming and Unity

Arrow left icon
Product type Paperback
Published in Aug 2020
Publisher Packt
ISBN-13 9781800207806
Length 366 pages
Edition 5th 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 (16) 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. Exploring Generics, Delegates, and Beyond 13. The Journey Continues 14. Pop Quiz Answers 15. Other Books You May Enjoy

Time for action – creating a variable

Alright, enough theory; let's create a real variable in our LearningCurve script:

  1. Double-click on LearningCurve to open it in Visual Studio and add lines 7, 12, and 14 (don't worry about the syntax right now – just make sure your script is the same as the script that is shown in the following screenshot):

  1. Save the file using command + S on a Mac keyboard, or Ctrl + S on a Windows keyboard.

For scripts to run in Unity, they have to be attached to GameObjects in the scene. HeroBorn has a camera and directional light by default, which provides the lighting for the scene, so let's attach LearningCurve to the camera to keep things simple: 

  1. Drag and drop LearningCurve.cs onto the Main Camera.
  2. Select the Main Camera so that it appears in the Inspector panel, and verify that the LearningCurve.cs (Script) component is attached properly.
  1. Click Play and watch for the output in the Console panel:

The Debug.Log() statements printed out the result of the simple math equations we put in between the parentheses. As you can see in the following Console screenshot, the equation that used our variable worked the same as if it was a real number:

We'll get into how Unity converts C# scripts into components at the end of this chapter, but first, let's work on changing the value of one of our variables.

You have been reading a chapter from
Learning C# by Developing Games with Unity 2020 - Fifth Edition
Published in: Aug 2020
Publisher: Packt
ISBN-13: 9781800207806
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 €18.99/month. Cancel anytime