Visual Studio Code C# Hello World



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).

  1. πŸ”₯πŸ”₯ Best Online Code Training: πŸ”₯πŸ”₯ BEST Web Hosting: Screen.
  2. 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:

  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.

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.

'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.

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 .
'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.

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.

'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).

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:

  1. 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).

'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

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.

Visual Studio Code C# Hello World

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

  1. Opening the Explorer menu (top most item on the vertical menu on the far left)
  2. Select the Open Folder button
  3. 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.