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.
Currently, I'm trying to write C/C program in Visual Studio code. For this I've installed two extensions: C/C & C Intellisense. As per the documentation, the debugging facility is not available for windows. I've been able to build and run the code with the following tasks. Create your service. In your command prompt, run the following command to create your app: In your terminal, run the following command to create your app: Command prompt. Dotnet new webapi -o MyMicroservice -no-https -f net5.0. Then, navigate to the new directory created by the previous command: Command prompt. Visual Studio currently provides the option to debug code outside your project source code, such as.NET or third-party code your project calls by specifying the location of the.pdb (and optionally, the source files of the external code).
- π₯π₯ Best Online Code Training: π₯π₯ BEST Web Hosting: Screen.
- The Main program called Program.cs is default code file which is created when a new application is created in Visual Studio. This code will contain the necessary code for our console application. Step 3) Now let's write our code which will be used to display the string 'Hello World' in the console application.
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:
- 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.
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.
With either of these two methods you can open a Project.
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.
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).
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 .
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.
Visual Studio Code C# Hello World Code
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:
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();β 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.
Once this circle disappears, we can continue working within Unity.
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:
- 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).
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.
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).
Now our Script is already part of an object of our game.
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:
- In the Menu bar, go to Fileβ Build Settingsβ¦
2. The βBuild Settingsβ window will be shown:
Visual Studio Code C# Hello World
Note: Within the βPlatformβ section, βPC, Mac & Linux Standaloneβ must 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.
Visual Studio Code C# Hello World Online
4. Close the βBuild Settingsβ window.
The Time Has Come! Press the βPlayβ Button!
In the βConsoleβ window, you will see the βHello Worldβ message π
Visual Studio Code C# Hello World Tutorial
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).
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:
- Create a new script and call it βMyNameβ.
- 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).
- Create a new empty game object in your Unity Scene and add your script.
- Execute the scene and see what happens.
- Repeat step #2, but now write your code inside βvoid update () {β¦}β, save your changes, run the scene and see what happens π
- 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
Example
Hello World C Visual Studio Code
This example introduces you to the basic functionality of VS Code by demonstrating how to write a 'hello world' program in C++. Before continuing, make sure you have the 'ms-vscode.cpptools' extension installed.
Initialize the Project
The first step is to create a new project. To do this, load the VS Code program. You should be greeted with the typical welcome screen:
To create the first program, select 'Start' > 'New file' from the welcome screen. This will open a new file window. Go ahead and save the file ('File' > 'Save') into a new directory. You can name the directory anything you want, but this example will call the directory 'VSC_HelloWorld' and the file 'HelloWorld.cpp'.
Now write the actual program (feel free to copy the below text):
Great! You'll also notice that because you've installed the 'ms-vscode.cpptools' extension you also have pretty code-highlighting. Now let's move on to running the code.
Running the Script (basic)
We can run 'HelloWorld.cpp' from within VS Code itself. The simplest way to run such a program is to open the integrated terminal ('View' > 'Integrated Terminal'). This opens a terminal window in the lower portion of the view. From inside this terminal we can navigate to our created directory, build, and execute the script we've written. Here we've used the following commands to compile and run the code:
Notice that we get the expected Hello World!
output.
Running the Script (slightly more advanced)
Great, but we can use VS Code directly to build and execute the code as well. For this, we first need to turn the 'VSC_HelloWorld' directory into a workspace. This can be done by:
Visual Studio Code C# Hello World App
- Opening the Explorer menu (top most item on the vertical menu on the far left)
- Select the Open Folder button
- Select the 'VSC_HelloWorld' directory we've been working in. Note: If you open a directory within VS Code (using 'File' > 'Open...' for example) you will already be in a workspace.
The Explorer menu now displays the contents of the directory.
Next we want to define the actual tasks which we want VS Code to run. To do this, select 'Tasks' > 'Configure Default Build Task'. In the drop down menu, select 'Other'. This opens a new file called 'tasks.json' which contains some default values for a task. We need to change these values. Update this file to contain the following and save it:
Note that the above also creates a hidden .vscode directory within our working directory. This is where VS Code puts configuration files including any project specific settings files. You can find out more about Taskshere.
In the above example, ${workspaceRoot}
references the top level directory of our workspace, which is our 'VSC_HelloWorld' directory. Now, to build the project from inside the method select 'Tasks' > 'Run Build Task...' and select our created 'build' task and 'Continue without scanning the task output' from the drop down menus that show up. Then we can run the executable using 'Tasks' > 'Run Task...' and selecting the 'run' task we created. If you have the integrated terminal open, you'll notice that the 'Hello World!' text will be printed there.
It is possible that the terminal may close before you are able to view the output. If this happens you can insert a line of code like this int i; std::cin >> i;
just before the return statement at the end of the main()
function. You can then end the script by typing any number and pressing <Enter>.
And that's it! You can now start writing and running your C++ scripts from within VS Code.