- Learning C# by Developing Games with Unity 2020
- Harrison Ferrone
- 1006字
- 2025-04-04 12:50:21
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 eventually run them, 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.
There are several ways to create C# scripts from the editor:
- Select Assets | Create | C# Script.
- In the Project tab, select Create | C# Script.
- Right-click in the Project tab (on the right-hand side) and select Create | C# Script from the pop-up menu.
- Select a 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 marked folders. This isn't just a Unity-related task – it's something you should always do, and your coworkers will thank you (I promise):
- Click Create | Folder (or whichever method you like best) from the Project tab and name it Scripts:

- 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:

We've just created a subfolder named Scripts, as shown in the preceding screenshot. Inside that parent folder, we've 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 it up automatically when you double-click any C# script from inside the editor.
Time for action – 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 Projects tab.
Double-click on LearningCurve.cs, as follows:

This will open up the C# file in Visual Studio:

If the file opens up in another default application, follow these steps:
- Select Unity | Preferences from the top menu and choose External Tools in the left-hand panel.
- Change the External Script Editor to Visual Studio, as shown in the following screenshot:

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. There are far more features to the Visual Studio application, but this is all we need to get the programmatic ball rolling.
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 the previous screenshot 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. The filename would change, but line five 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 go into Visual Studio and change NewBehaviourScript to the name of your C# script.
Syncing C# files
As part of their symbiotic relationship, Unity and Visual Studio keep in touch 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 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, right-click, and select Refresh.
You now have the basics of script creation under your belt, so it's time we talk about finding and efficiently using helpful resources.