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

You're reading from   Learning C# by Developing Games with Unity Get to grips with coding in C# and build simple 3D games in Unity 2023 from the ground up

Arrow left icon
Product type Paperback
Published in Nov 2022
Publisher Packt
ISBN-13 9781837636877
Length 466 pages
Edition 7th 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 FREE CHAPTER 2. The Building Blocks of Programming 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. Specialized Collection Types and LINQ 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

Using C# with Unity

Going forward, it’s important to think of Unity and C# as symbiotic entities. Unity is the engine where you’ll create scripts and GameObjects, but the actual programming takes place in another program called Visual Studio.

Working with C# scripts

We haven’t covered any basic programming concepts yet, but they won’t have a home until we know how to create an actual C# script in Unity. A C# script is a special kind of C# file in which you’ll write C# code. These scripts can be used in Unity to do virtually anything, from controlling an in-game character with your keyboard, to animating objects in your level.

There are several ways to create C# scripts from the editor:

  • Select Assets > Create > C# Script
  • Right under the Project tab, select the + icon and choose C# Script
  • Right-click on the Assets folder in the Project tab and select Create > C# Script from the pop-up menu
  • Select any GameObject in the Hierarchy window and click Add Component > New Script

Going forward, whenever you’re instructed to create a C# script, please use whichever method you prefer.

Resources and objects other than C# scripts can be created in the editor using the preceding methods. I’m not going to call out each of these variations every time we create something new, so just keep the options in the back of your mind.

For the sake of organization, we’re going to store our various assets and scripts inside their own named folders. This isn’t just a Unity-related task—it’s something you should always do, and your co-workers will thank you (I promise):

  1. Select Assets > Create > Folder and name it Scripts:

    Figure 1.15: Creating a C# script

  2. Double-click on the Scripts folder and create a new C# script. By default, the script will be named NewBehaviourScript, but you’ll see the filename highlighted, so you have the option to immediately rename it. Type in LearningCurve and hit Enter:

    Figure 1.16: Project window with the Scripts folder selected

  3. You can use the small slider in the bottom right of the Project tab to change how your files are displayed.

So, you’ve just created a subfolder named Scripts, as shown in the preceding screenshot. Inside that parent folder, you created a C# script named LearningCurve.cs (the .cs file type stands for C-Sharp, in case you were wondering), which is now saved as part of our Hero Born project assets. All that’s left to do is open it up in Visual Studio!

Introducing the Visual Studio editor

While Unity can create and store C# scripts, they need to be edited using Visual Studio. A copy of Visual Studio comes pre-packaged with Unity and will open up automatically when you double-click any C# script from inside the editor.

Opening a C# file

Unity will synchronize with Visual Studio the first time you open a file. The simplest way to do this is by selecting the script from the Project tab. Take the following steps:

  1. Double-click on LearningCurve.cs, which will open up the C# file in Visual Studio:

    Figure 1.17: LearningCurve C# script in Visual Studio

  2. You can change the Visual Studio tabs at any time from Visual Studio | View | Layout. I’ll be using the Design layout for the rest of the book so we can see our project files on the left-hand side of the editor.
  3. You’ll see a folder structure on the left-hand side of the interface that mirrors the one in Unity, which you can access like any other. On the right-hand side is the actual code editor where the magic happens (all the code you write will live here). There are far more features to the Visual Studio application, but this is all we need to get started.

The Visual Studio interface is different for Windows and Mac environments, but the code we’ll be using throughout this book will work equally well with both. All the screenshots in this book have been taken in a Mac environment, so if things look different on your computer, there’s no need to worry.

Beware of naming mismatches

One common pitfall that trips up new programmers is file naming—more specifically, naming mismatches—which we can illustrate using line 5 from Figure 1.17 of the C# file in Visual Studio:

public class LearningCurve : MonoBehaviour

The LearningCurve class name is the same as the LearningCurve.cs filename. This is an essential requirement. It’s OK if you don’t know what a class is quite yet. The important thing to remember is that, in Unity, the filename and the class name need to be the same. If you’re using C# outside of Unity, the filename and class name don’t have to match.

When you create a C# script file in Unity, the filename in the Project tab is already in Edit mode, ready to be renamed. It’s a good habit to rename it then and there. If you rename the script later, the filename and the class name won’t match.

If you were to rename the file at a later point, the filename would change, but line 5 would be as follows:

public class NewBehaviourScript : MonoBehaviour

If you accidentally do this, it’s not the end of the world. All you need to do is right-click on the script in the Projects tab and choose Rename:

Figure 1.18: Renaming a C# script

Syncing C# files

As part of their symbiotic relationship, Unity and Visual Studio communicate with each other to synchronize their content. This means that if you add, delete, or change a script file in one application, the other application will see the changes automatically.

So, what happens when Murphy’s Law, which states that “anything that can go wrong will go wrong,” strikes and syncing just doesn’t seem to be working correctly? If you run into this situation, take a deep breath, select the troublesome script in Unity, right-click, and select Refresh.

Figure 1.19: Refreshing a C# script

You now have the basics of script creation under your belt, so it’s time we talk about finding and efficiently using helpful resources.

You have been reading a chapter from
Learning C# by Developing Games with Unity - Seventh Edition
Published in: Nov 2022
Publisher: Packt
ISBN-13: 9781837636877
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