• Scripts and writing log to console
Scripts and writing log to console

Welcome back gamemakerlab! 

Did you remember the latest post of gamemakerlab which guided you how to Create and use Sprites (2D) ?

Now, Gamemakerlab will guide you something about Unity scripts and how to write a simple string “Hello world!” into Console window by using C# Script.

If you’ve never learnt any programming language before, just relax and focus on my code, we’ll explain everything inside it for you. Don’t hesitate to post a comment to ask us any question about it, we will try to help you as much as we can.

SCRIPTS IN UNITY

We can be sure that not only Unity but all other game maker tools need code to create game, there is only one thing can be different and that is the coding language. So if you want to make a game, you have to know about programming, that’s the truth.

In Unity, it has many different languages to use, as long as that language can create a DLL (dynamic link library) file, but the two most common languages are C# (C – sharp) and JavaScript.

In our tutorial series, we are using C#, it’s a popular language and easy to learn (I think).

Unity has its own IDE for programming, it’s called Mono developer, you can also use Microsoft Visual Studio to write code (I’m using it), it can be downloaded and installed in the same time with Unity. For detail, please take a look at another tutorial from us.

 

WRITING A LOG INTO CONSOLE SCREEN

Now we will give you an example.

This project was created in the previous tutorial, you can check it out or download it from this link.

This is our current state, now we will try to write a log to Console window (the red rectangle) when the game start.

First, we need to create a script.

In the Project window, right click on Assets folder -> Create -> Folder -> Name it as Scripts

Right click on this folder -> Create -> C# Script

Set name for it as whatever you like.

Double click on it to open a Scripts editor.

 You can select which editor to open like this

Select Edit on menu bar -> Preferences

Select External Tools on the left -> Select External Script Editor then close.


After you double clicked on the script and the Script Editor has been opened. It will show up like this:

We are trying to write a log on Console window when the game start, so all we need to do is just put a single line of code inside Start method, this method will be called automatically when the game start.

void Start ()

{

Debug.Log("Hello world!"); //This Debug class is provided by Unity engine

      //The same as print("Hello world!");

}

Press Ctrl + S to save or press F6 (Ctrl + Shift + B) to build project.

Come back to Unity.

Attach this script into an object of current scene as a component.

We already have 3 objects inside current scene.

 

Now let’s attach our script to the Main Camera.

Select Main Camera in Hierarchy window -> Look at Inspector window

Click on Add Component

 

 Select Script                                                          

 Select the script we just created

  

Now click on this triangle button to test

The Console window should be like this:

Ok, we’ve done. That’s how to make Unity write a string into Console window. Remember that you can also write value of any variable too, not only string. Well do it in our next tutorials.

Thanks for reading!

In the next tutorial, we’ll show you how to make an object move around. See you!