Simple Leveling Up System in Unityscript – Unity 5 Beginner Scripting Tutorial

Before we get started, I’m going to assume you already know how to make a new script, link the script to a game object and edit the script. This tutorial will not explain how you would do that. If you want to first get an understanding of how the interface works then read the getting started post.

Get Set Up

This tutorial is for unity script so first you will have to make a new javascript file. Once you have done this, you will need to make a new empty object then once selected add a new script component in the inspector window. Then click on the cog symbol above the script component and choose edit script.

Leveling System

Lets get scripting!

So now that your all set up, you should now have monodevelop or which ever script editor program you use open. So we start off with this script automatically in the file:

 #pragma strict

function Start () {

}

function Update () {

}

All this basically means is you have a function for when the script is started and one for every update. Anything between the curly brackets will either run once at start or every frame update depending on where you place your script. Now lets add some variables. A variable is a container of information which can be changed.

#pragma strict

var xp :int = 0;

var level:int = 1;

function Start () {

}

function Update () {

}

So what you have done is declared a “var” with the name “xp” which is a “:int” (int is short for integer which is a number) and carries the value of “0”. So now that we have our two variables let’s out put the values. The easiest way we can do this is to use print.

#pragma strict

var xp :int = 0;

var level:int = 1;

function Start () {

}

function Update () {

print(xp);

print(level);

}

As we want it to continuously update the results of these variable’s you will need to place it in between the curly brackets. Now we want to be able to increase the amount of xp. For us to do this we need to give some sort of interaction to increase the xp. This could be done when a player kills an enemy or finish’s a quest. To do that we would also need a enemy and fighting system or a quest system. As we don’t have these I will keep it simple and use a mouse click.

#pragma strict

var xp :int = 0;

var level:int = 1;

function Start () {

}

function Update () {

print(xp);

print(level);

if(Input.GetMouseButtonDown(0)){
xp=xp+10;
}

}

What i have added is :

if(Input.GetMouseButtonDown(0)){
xp=xp+10;
}

to the update function. This basically says “if” the “left mouse button is down” then “set the variable xp’s value” to “the value of xp” plus “10”. So now if you run the game. You will be able to see that xp increases every time you click the left mouse button. Make sure you look at the console panel to see the change. Now that we have the xp going up. Let’s make it level up at a 100 xp.

#pragma strict

var xp :int = 0;

var level:int = 1;

function Start () {

}

function Update () {

print(xp);

print(level);

if(Input.GetMouseButtonDown(0)){
xp=xp+10;
if(xp == 100){
xp = 0;
level = level+1;
}
}

}

So now i have added this when the mouse button is clicked:

if(xp == 100){
xp = 0;
level = level+1;
}

This says if “xp value” is equal to “100” then set “xp value” to “0” and  set “level value” to “level value + 1″. So far so good, it should now reset to 0 at a 100 xp and level you up. This is great but we want the leveling to be hard each time you level up. So we need to amend what we just already.

#pragma strict

var xp :int = 0;

var level:int = 1;

function Start () {

}

function Update () {

print(xp);

print(level);

if(Input.GetMouseButtonDown(0)){
xp=xp+10;
if(xp == 100*level){
xp = 0;
level = level+1;
}
}

}

So now i have amended the if statement to times 100 by what level you are. Now when you test your game each level will take longer to level up. Hopefully this will have taught you some fundamental scripting lessons such as if statements, functions and variables. These core scripting elements have millions of uses and as you advance you will start to see their importance in scripting.