Unity Tutorial for Beginners. C Sharp in Unity. Image from Ackosmic Games

2. “Hello World” in Unity

In this Unity Tutorial we will learn How to Create our First Code in "C Sharp (C #)" language and How to Execute it in Unity to Display the Famous Message “Hello World” 🙂

Unity Tutorial Level: Beginner.

2.1 Opening our Project.

In the previous post (1. Setting the Environment), we created the Project “LearningCSharp” (in which we will be working during this tutorial series).

In case you have closed the Project, here are two ways to open it again:

  1. When we open Unity in our computer, the “Home” window appears. In the “Projects” Section will be shown a list of the most recent Projects in which we have worked; and here our “LearningCSharp” Project must be shown, we just have to click on it.

Unity Tutorial. Unity Home Image from Ackosmic Games

2. In case the Project is not shown, by clicking on the “Open” button in this same window, you will be asked to find the path in which the “Folder” with our Project's Name is located; you have to select only the folder and then click on the “Select folder” button.

Unity Tutorial. Unity Home Image from Ackosmic Games

With either of these two methods you can open a Project.

"Ads" (the Ads Help us to Maintain this "Great Site" 😀 )

2.2 Creating our First “Script”.

Now we are going to create our first Code File (“Script”) in Unity.

With our Project open, go to the “Project” window and right click on your “Scripts” folder (remember that “Scripts” folder is inside “Assets”); from the menu that is displayed, select Create  → C# Script.

Unity Tutorial. C Sharp (C#). Unity Editor Interface Image from Ackosmic Games

Once this is done, a file will be created inside “Scripts” folder, this file will have its name ready to be modified, so before clicking anywhere else, it is better to assign it a name. We recommend “HelloWorld” (without space between words, then we´ll see why).

Unity Tutorial. C Sharp (C#). Unity Editor Interface Image from Ackosmic Games

Now, we can click on any other place or just press “Enter” and our Script will take the assigned name.

Note: Naming a Script has some basic rules to follow:

  • Only use Alphanumeric characters (no symbols or spaces because they can create conflict with other instructions).
  • Do not start the name with numeric characters (these are valid within the name, but not as the first character).
  • Use Uppercase and Lowercase (use “Pascal Case” code). Uppercase at the beginning of the file name and at the beginning of each Word (example: HelloWorld, FirstScript, PlayerController).
  • Do not use as a name, system specific functions (from Unity or C# libraries).
  • For more information visit https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/general-naming-conventions .
"Ads" (the Ads Help us to Maintain this "Great Site" 😀 )

2.3 Writing Scripts.

To modify the content of our Script (and be able to create our amazing codes), we must use a compatible Code Editor, and this must be linked with Unity; in our case, we will use “Visual Studio Code”. If you still do not have it installed on your computer and would like to work with it, visit the post “Installing Visual Studio Code for Unity” where we will guide you step by step in its installation.

Let´s start our code editor in the following way (a very simple one): let's double click on the “HelloWorld.cs” file (inside the Unity’s “Project” window). With this, the following window will be shown:

Unity Tutorial. C Sharp (C#). Unity Visual Studio Code Image from Ackosmic Games

This is our code editor (Visual Studio Code), on the right side (where the text is in Green color) you can see the default content that a Script has when it is created.

In the following tutorials, we will see more deeply each of the parts that set our Script up, for now we will only make the “Hello World” message appears on the Unity’s “Console” window.

Between the “brackets” symbols ( “{ }” ) that are found after “void Start( )”, let's write:

Debug.Log(“Hello World”);

 

Debug.Log();” will be our tool (Method) that we will use to send messages to the Unity's Console when our code is executed; we usually use it when we want to know if a specific section of our code is running (for example, when we want to track errors).

Note: Do not forget to include the “semicolon” “;” after each method.

Now let's save our code; we can do it by pressing on our keyboard at the same time “Ctrl” and “S” (or from the menu bar File  → Save).

 

When switching to the Unity's Editor Interface, you will see that in the lower right corner (below the window, on the right side), something like a "spinning circle" will appear, this circle tells us to “wait” while Unity takes the changes made in the Code Editor.

Unity Tutorial. C Sharp (C#). Unity Editor Interface Image from Ackosmic Games

Once this circle disappears, we can continue working within Unity.

"Ads" (the Ads Help us to Maintain this "Great Site" 😀 )

2.4 Running the Script.

For being executed, our Script must be part of an object (a “Game Object”) in the Unity Scene; therefore, we will do the following:

  1. Inside the “Hierarchy” window, in some empty space, "right click” and select “Create Empty”. This will create an object called “GameObject” (an empty object, which can contain any element we want to be part of our games).

Unity Tutorial. C Sharp (C#). Unity Editor Interface Image from Ackosmic Games

2. When selecting “Game Object”, we will see that the “Inspector” window changes and now shows some data, this data is the Game Object's Properties.

Unity Tutorial. C Sharp (C#). Unity Editor Interface Image from Ackosmic Games

3. Inside this “Inspector” window, is necessary to click on the “Add Component” button, from the list that will be shown, select “Scripts” and then will be shown a list with all the code files (“scripts”) that we have in our Project, there, click on your “Hello World” script (note that this name shows a space between the words “Hello” and “World”; the system automatically adds a space each time it notices a capital letter in the name of a script).

Unity Tutorial. C Sharp (C#). Unity Editor Interface Image from Ackosmic Games

Unity Tutorial. C Sharp (C#). Unity Editor Interface Image from Ackosmic Games

Now our Script is already part of an object of our game.

Unity Tutorial. C Sharp (C#). Unity Editor Interface Image from Ackosmic Games

Note: Another way to add a code file to an object in the game is by “holding and dragging the script” from the “Project” window (and from the “Scripts” folder) and “dropping it” into an empty space in the “Inspector” window (or “dropping it” on the desired “game object’s name” in the “Hierarchy” window).

Before pressing the “Play” button, is necessary to add the Unity Scene to the list of scenes that will be compiled in our project. For that, do the following:

  1. In the Menu bar, go to File→ Build Settings…

Unity Tutorial. C Sharp (C#). Unity Editor Interface Image from Ackosmic Games

2. The “Build Settings” window will be shown:

Unity Tutorial. C Sharp (C#). Unity Editor Interface Image from Ackosmic Games

Note: Within the “Platform” section, “PC, Mac & Linux Standalonemust be selected (which means that we are making games to be executed on computers, then in future tutorials we will see how to make games for other platforms), as well this must be indicated at the front of the section (as you could see in the image). Otherwise (if another platform is selected), you have to select this option (“PC, Mac & Linux Standalone”) and click on the “Switch Platform” button (the button is at the bottom of the list).

3. Click on the “Add Open Scenes” button and you will see how the “CSharpScene” scene (the work scene we created in the previous tutorial) is added to the scene's list that will be compiled for our project.

Unity Tutorial. C Sharp (C#). Unity Editor Interface Image from Ackosmic Games

4. Close the “Build Settings” window.

The Time Has Come! Press the “Play” Button!

Unity Tutorial. C Sharp (C#). Unity Editor Interface Image from Ackosmic Games

In the “Console” window, you will see the “Hello World” message   🙂

Note: By pressing the “Play” button, you also see how the window changes from “Scene” to “Game” view, showing a blue background (the one our users would see if they executed the game now), there is nothing to worry about, this is normal because our game still has no graphic elements to show (except for the blue background).

If you press the “Play” button again, the execution of the scene will stop, and every time you repeat this action you will see how a new “Hello World” messages are appended in the Unity’s Console. To delete them you just have to click on the “Clear” button that is below the Console window’s name.

 

Before finishing, is mandatory to save our work (go to File→Save Scenes and then File→Save Project).

"Ads" (the Ads Help us to Maintain this "Great Site" 😀 )

Exercises.

For getting a stronger knowledge about what you have already learned, it is necessary to practice it, so try to perform the following exercises:

  1. Create a new script and call it “MyName”.
  2. Open your new script in the code editor and make it shows your full name in the Unity' Console when the scene is executed (remember to write your code inside “void Start () {…}” and then save your changes).
  3. Create a new empty game object in your Unity Scene and add your script.
  4. Execute the scene and see what happens.
  5. Repeat step #2, but now write your code inside “void update () {…}”, save your changes, run the scene and see what happens  😀
  6. Modify your code again as it was written in this tutorial.

This Unity Tutorial about how to Create a Script, Modify it using the Code Editor, Add it to a game object within the Unity Scene and Execute it to show the message “Hello World” ends here. Join us in the next post where we will continue learning more about programming in C Sharp (C#) Language.

Remember, if you want to know more about this topic, you can always visit Unity's online user manual at https://docs.unity3d.com/Manual/ or, do not hesitate to contact us for any questions or advice by clicking “Here”.

Next Unity Tutorial: 3. Knowing our Script

"Ads"
Share this Post
Posted in CSharpAndUnity.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.