Unity3D

Unity3D Tutorial: Create Component and Hello World Program

Unity 3D Hello world Program

Introduction:

In this tutorial, we will learn the User interface of unity 3D. we will also see that how we can show text when the game runs and how we can access different components in unity.

Interface:

When we first open our unity project we will see a screen like this.

 

Unity3D Tutorial: How to Create Components and write hello world program in unity

 

In the middle we have our scene view, we can use the mouse wheel to zoom in and out, middle mouse button to pan around and if we hold down alt and left click we can then pivot around the center of our view. If we hold down right click we can then look around by using the mouse and move in the scene by using WASD keys.

 

 

On the left we have hierarchy this displays all the different game objects that we have in our scene. Here we have two objects in our scene i.e. Main Camera and Directional Light.

 

 

On the right, we have the inspector that displays all the properties of a selected game object if we select the Main Camera from the hierarchy we can see all of its properties in the inspector

 

 

In the bottom, we have our project panel that shows all of our game Assets. We can drag these assets into our scene from here which will make them appear in the hierarchy.

 

Components:

Components are found in the Inspector e.g Transform, Camera etc. It is what make objects to do different things.

We will create our own component in form of the script so right click in the project panel then Create > C# script.

 

 

In our case, we have created a C# file with name ‘TestScript’ you can notice in the above screenshot.

If you double-click on this TestScript file it will open visual studio or mono develop or any other editor that you have. Here you can see our script file in the below screenshot.

 

 

You can notice in above screenshot that Unity by default imported some libraries and created a class and methods.

Our Script has a class with name TestScript which have mono Behaviour. By default, we have got two methods with name start() and update().

The first method we have got is a start method followed by the rounded bracket and then curly brackets. Whatever code you will write in between these curly brackets it will be executed when the game starts.

 

Void Start(){

}

 

The second method we have got is the Update method. It is used to executes the code on each frame of the game. That means whenever the computer draws a new frame and it does it multiple times a second this function is going to run.

 

Void Update(){

}

 

Now we are going to create a simple Hello World Program. To do that we need to write following line of code in the start method.

 

Void Start(){

Debug.Log(“Hello World!”)

}

 

Now save the file and go back to unity and choose one of the game objects and attached that object to Script. So to add the script to the game object, select the game object first and then drag and drop the script in that object.

 

 

You can also add a script to the game object through Add component. Just click on Add Component button and type the script name in search bar. Here is the screenshot below:

 

 

This TestScript is now the component of this game object. Now when you run the game we will get the Hello World text written in our console.

 

 

You can notice that we have ‘Hello World’ in our console only once. It is because the start method runs only one time. If we will write Debug.Log(“Hello World!”) in the update method, then we will get hello world written on our console at every frame.

We can also print Hello World without script too. Keep in mind that all the parameters are basically some kind of variable. Mass, Drag, angular drag etc. are variable.

 

 

So, let’s go ahead and create a variable, in our case we have declared a public string name variable. The public variable means that other game object can access this variable. This is how our code look like:

 

 

Now, save the Script file and go back to unity. If we run the game nothing will be printed on the console. It is because our game component TestScript does not have a parameter. We will write some string in the name Parameter, like in our case we wrote: “Hello There!”. Here is the screenshot below:

 

 

Now if we run the unity then we will get the ‘Hello There!’ written in our console.

 

Conclusion:

In this Unity tutorial, we have given an overview of the unity interface. we have also learned how we can create a Script file in unity and run a simple hello world program.

 

Click to comment

Most Popular

To Top