How to set up Visual Studio Code for Unity

All of the Visual Studio Code Extensions you'll need to get started using it with Unity3D.

Downloading and Installing Visual Studio Code

If you don't already have Unity installed, take a look at How to Install Unity3D and Unity Hub!

Why Visual Studio Code?

Our code editor of choice is Microsoft Visual Studio Code. It’s not to be mixed up with Microsoft Visual Studio. Both are similar (and similarly named) products from the same company, both are free to use, and you could use either one to do the job. They can both edit C# code and integrate with Unity. Visual Studio Code is designed to be cross-platform and lightweight out of the box, but highly extensible. It has a minimalistic user interface, and most of its features are enabled through installing extensions to add extra functionality, which you do through the software itself.

Downloading Visual Studio Code

To download Visual Studio Code, head on over to this link in your favorite web browser.

From there, you can select the correct software for your operating system. The installer should begin to download. Once it completes, run it and follow the instructions it provides. Once you have Visual Studio Code running for the first time, you’ll see a get-started page serving as a hub for various links and resources. Many programmers are picky about the color scheme their code editor uses. I prefer dark schemes myself. Some prefer light – whatever floats your boat. You can easily change it right from the get-started page. Click the “Browse Color themes” button to pop up a list of standard color themes, allowing you to switch to whichever theme you prefer

Once you’re satisfied with your colors (you can always change them again later), let’s close the get-started page. Any file or page you have open in Code will have a tab at the top left (see Figure ) whether it’s an editable code file you’re working on or a static page like the get-started page. If you have multiple files open, you can easily switch to view a different one by clicking the tab. Right now, we only have the get-started page open. Let’s close it to get a blank slate. You can do this by left-clicking the X button on the tab or by using the Ctrl+W hotkey.

Once it’s closed, there won’t be much going on – just a big blank space, as shown in the following figure.

Adding necessary Extensions to Visual Studio Code

You can now install what Visual Studio Code calls “extensions” to add some extra functionality to the editor. Extensions are managed and installed through a button on the left sidebar. The left sidebar has a handful of different forms it can take, all based on which of those buttons you pressed last. Mouse over the buttons to see what they mean and click the Extensions button when you find it (it’s the bottom one). You can also press Ctrl+Shift+X to bring it up. This will cause the sidebar to pop up on the left side of the screen. Pressing the button again will fold the sidebar, tucking it away

Inside the Extensions sidebar, you can search for extensions with the search bar at the top. Click within the search bar and type “C#”. You should see a result simply titled “C#”, with the description “C# for Visual Studio Code.” You’ll also notice that the publisher for the extension is listed beneath the description: “Microsoft,” which so happens to be the company behind Visual Studio Code and C# itself.

Click this extension, and a new tab will pop up, providing details about the extension. Under the main description at the top of the page, you’ll see a button to install the extension. Click that, and the extension will begin installing. If you’re prompted to install any further extensions by popup boxes during this, go ahead and permit them to install.

Next, we’ll install the extension for debugging in Unity. This allows us to “attach” our code editor to Unity so that we can use the code editor to set up “breakpoints” in the code. A breakpoint is a point in the code that, when reached during the execution of the program, causes the whole program to freeze. While frozen, we can look at pretty much any piece of data from our program that we want and resume whenever we please, among other things. It’s a very handy feature that we’ll use later down the road.

The exact name of the extension is “Debugger for Unity,” published by Unity Technologies. You can find it the same way – by typing the name in the search bar at the top of the Extensions sidebar. The Debugger extension is currently deprecated, but still functional. You can read more about it here.

Connecting Visual Studio Code to Unity as Its script Editor

Once you have these extensions installed, we can close Visual Studio Code. For now, we’re focused on connecting Visual Studio Code to Unity as Its Script Editor.

In order to connect Visual Studio Code to Unity, you need to open the Unity Editor. For that Go to Unity Hub and create a new project. Next, provide the name of your project, choose the project location, and select the project template. In my case, I choose 3D Mobile because I want to develop a 3D android game.

The project Initialisation will take a couple of minutes. You will be redirected to the Unity Editor with your project starting point.

To connect Unity Editor to Visual Studio Code, on Windows go to the ‘Edit’ > ‘Preferences’. On Mac, go to ‘Unity’ > ‘Preferences’. You will see a screen with the title ‘preferences’. Next, Go to the “External Tools” menu, then select ‘Visual Studio Code on the ‘“External Script Editor” dropdown menu. If you don’t see ‘Visual Studio Code’ on this menu, just select the folder where you have installed Visual Studio Code.

Now, Unity Editor is connected to Visual Studio Code. To verify it, create a new script called ‘testscript’ by clicking on the plus ‘+’ in the project section. Then select ‘C# Script’.

Next, double-click on the ‘testscript’ file. Visual Studio Code will open with the start file of your new project.

The final step in Getting Started in Unity3D with Visual Studio Code is to install the Main .Net SDK. Unity uses the open-source Main .NET SDK to ensure that applications you make with Unity can run on a wide variety of different hardware configurations. To download .Net SDK, head on over to this link in your favorite web browser: https://dotnet.microsoft.com/en-us/download

From there, download and install the latest version. Once the installation is finished, restart your computer.

Congratulations! You now have Unit3D initialized with Visual Studio Code. You can start your revolutionary project with Unity.