Getting to Know Your Environment
Pop culture often markets computer programmers as outsiders, lone wolves, or geeky hackers. People possessing extraordinary mental gifts for algorithmic thought, little social IQ, and the odd anarchic bent. While this is not the case, there is something to the idea that learning to code fundamentally changes the way you look at the world. The good news is your naturally curious mind already wants to see these kinds of patterns in the world, and you may even come to enjoy this new way of thinking.
From the moment your eyes snap open in the morning to the last glimpse of your ceiling fan before you go to sleep, you're unconsciously using analytical skills that translate to programming—you're just missing the right language and syntax to map those life skills into code. You know your age, right? That's a variable. When you cross the street, I presume you look down the road in both directions before stepping off the curb like...
Technical requirements
Sometimes it's easier to start with what a thing isn't, rather than what it is. The goal of this book isn't to teach you everything there is to know about the Unity game engine or game development. By necessity, we'll cover these topics at a basic level at the beginning of our journey, and in more detail in Chapter 6, Getting Your Hands Dirty with Unity. However, these topics are included to provide a fun, accessible way to learn the C# programming language from the ground up.
Since this book is aimed at complete beginners to programming, if you have no previous experience with either C# or Unity, you're in the right place! If you've had some experience with the Unity Editor but not with programming, guess what? This is still the place to be. Even if you've dabbled in a bit of C# mixed with Unity, but want to explore some more intermediate or advanced topics, the later chapters of this book can provide you with what you...
Getting started with Unity 2021
If you don't have Unity installed, or are running an earlier version, follow these steps to set up your environment:
- Head over to https://www.unity.com/.
- Select Get started (shown in the following screenshot):
Figure 1.1: Unity homepage
This will take you to the Unity store page. Don't feel overwhelmed by this—you can get Unity completely for free!
If the Unity homepage looks different for you than what you can see in Figure 1.1, you can go directly to https://store.unity.com.
- Select the Personal option. The other paid options offer more advanced functionality and services, but you can check these out on your own:
Figure 1.2: Unity plans and pricing
- After selecting the Personal plan, you'll be asked if you're a first-time or returning user. Select Start here under First-time users:
Figure 1.3: Start creating with the Unity...
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 game objects, but the actual programming takes place in another program called Visual Studio. Don't worry about that right now—we'll get to that in a moment.
Working with C# scripts
Even though we haven't covered any basic programming concepts yet, 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 responding to player input to creating game mechanics.
There are several ways to create C# scripts from the editor:
Exploring the documentation
The last topic we'll touch on in this first foray into Unity and C# scripts is documentation. Not sexy, I know, but it's important to form good habits early when dealing with new programming languages or development environments.
Accessing Unity's documentation
Once you start writing scripts in earnest, you'll be using Unity's documentation quite often, so it's beneficial to know how to access it early on. The Reference Manual will give you an overview of a component or topic, while specific programming examples can be found in the Scripting Reference.
Every game object (an item in the Hierarchy window) in a scene has a Transform component that controls its Position, Rotation, and Scale. To keep things simple, we'll just look up the camera's Transform component in the Reference Manual:
- In the Hierarchy tab, select the Main Camera game object
- Move over to the Inspector tab and click on...
Summary
We covered quite a bit of logistical information in this chapter, so I can understand if you're itching to write some code. Starting new projects, creating folders and scripts, and accessing documentation are topics that are easily forgotten in the excitement of a new adventure. Just remember that this chapter has a lot of resources you might need in the coming pages, so don't be afraid to come back and visit. Thinking like a programmer is a muscle: the more you work it, the stronger it gets.
In the next chapter, we'll start laying out the theory, vocabulary, and main concepts you'll need to prime your coding brain. Even though the material is conceptual, we'll still be writing our first lines of code in the LearningCurve
script. Get ready!
Pop quiz – dealing with scripts
- What type of relationship do Unity and Visual Studio share?
- The Scripting Reference supplies example code in regards to using a particular Unity component or feature. Where can you find more detailed (non-code-related) information about Unity components?
- The Scripting Reference is a large document. How much of it do you have to memorize before attempting to write a script?
- When is the best time to name a C# script?
JOIN us on Discord!
Read this book alongside other users, Unity/C# experts, and Harrison Ferrone. Ask questions, provide solutions to other readers, chat with the author via Ask Me Anything sessions and much more.
Join Now!
https://packt.link/csharpunity2021