From the course: Unity 3D 2019 Essential Training

Introduction to C# programming - Unity Tutorial

From the course: Unity 3D 2019 Essential Training

Start my 1-month free trial

Introduction to C# programming

- [Instructor] In this chapter we'll cover some of the basics of the scripting language used in Unity. But it is a simple introduction to get your feet wet, and not a full C# programming language overview. If you plan on creating games or any project that requires lots of scripting first, follow along in this chapter and then move on to a full C# course to learn more about the language. And then finish off by going back to Unity specific syntax and libraries available to you for scripting. Unity supports C# as its primary language, which will be familiar to you if you've ever used any of C based programming languages. So if you have a basic understanding of C or C++ languages, you should be fine working around the script we'll create soon. It supports other .NET languages, but I'd strongly recommend you stick to C#, since all documentation on Unity's site is based off this language. C# is an object-oriented language, and you can used the included Visual Studio IDE or any other editors to write C# code. In fact, when you create a new script inside of Unity and double-click it, it opens directly into Visual Studio or the editor you have set up in your preferences. You can do a lot with Unity without ever needing scripting, but when you want to create complex interactions, programming can't be avoided. You create the script inside of Unity's project and then attach it to a GameObject in your scene. Whenever an event needs to initiate the script functions, they are called from the GameObject, then the script executes. Typically C# will include the following parts. Using is a syntax to indicate we're importing external libraries for a code. And then we use namespacing to declare a collection of classes. In this case we're declaring HelloWorldApplication. Then we declare a class, which is basically what contains the data and methods your class uses. And then you move on to declare your Main method where you pass an argument and declare its type. So in this case we're passing an argument of string as the argument. And then through the comments we're basically writing now this is my first program in C#, and then we're using the console to write a line which is called Hello World. So with this program you would expect a line to appear in your console, and the line will be Hello World. Now again, we're not trying to teach you a full language in this chapter, but simply giving you a taste of the language, in case you decide to learn it.

Contents