- Home /
translated script from C# to JS; GUI not working properly?
I followed a script from a tutorial, but I wanted to convert it to JS, and, when I do so, the GUI elements don't working properly. I'm trying to make a level bar and an experience bar, and the experience bar gets bigger the more experience the player has. However, in JS, the bar is stuck at a tiny length, and will only go backwards. It works properly if I remove the "Screen.width" and simply use a number there instead, but the bar then gets proportionally longer every level, which is undesirable.
here is the C# script: using UnityEngine; using System.Collections;
public class Playerlevel : MonoBehaviour {
private int curLevel = 1;
private int maxLevel;
public int curExp = 1;
private int maxExp = 100;
public float expBarLength;
void Start ()
{
}
void Update ()
{
AdjustCurrentExp(0);
if(curExp >= maxExp)
{
curExp = 1;
curLevel++;
maxExp += (20 * curLevel);
}
}
void OnGUI()
{
GUI.Box (new Rect(20, 30, expBarLength, 20), curExp + "/" + maxExp);
GUI.Box (new Rect(20,70,200, 20 ), "Level" + curLevel);
}
public void AdjustCurrentExp(int adjExp)
{
curExp += adjExp;
expBarLength = (Screen.width/ 3) * (curExp / (float)maxExp);
}
}
and this is my JS translation:
pragma strict
var curLevel = 1;
var maxLevel = 10;
var curExp = 1;
var maxExp = 100;
var expBarLength;
function Update ()
{
AdjustCurrentExp(0);
if(curExp >= maxExp)
{
maxExp += (20 * curLevel);
curExp = 1;
if(curLevel < maxLevel)
{
curLevel++;
}
}
}
function OnGUI()
{
GUI.Box (Rect(20, 30, expBarLength, 20), curExp + "/" + maxExp);
GUI.Box (Rect(20,70,200, 20 ), "Level" + curLevel);
}
public function AdjustCurrentExp(adjExp: int)
{
curExp += adjExp;
expBarLength = (Screen.width/3) * (curExp / maxExp);
}
Well for starters, you didn't translate the code exactly the same. Lines 21-25 in the C# is different logic than the same code in the javascript version. The same with line 38.
If the javascript code works, then make the c# match it, same order, same variable values and everything. The syntax won't be exactly the same, but the code should be exactly the same as far as the logic goes.
With all that said, your c# code does nothing because you never increase the experience. calling AdujstCurrentExp(0) does nothing. Other than that, your c# version looks like it should work correctly.
When the user gains experience you should call AdjustCurrentExp() with the amount they gained and it seems like it should draw the right size rectangle
The only thing I'm having trouble with is the fact that when I increase curExp, in the C#, the bar gets bigger (works fine), but in JS nothing happens. (C# is the one that works, not JS)
for lines 21-25, I was experimenting with setting a max level. should've mentioned that, woops. I've set up the AdjustCurrentExp to be called and whatnot.
your comment did help me a lot on some of the other coding parts, though, so thanks!
Answer by tw1st3d · Jul 16, 2013 at 04:30 AM
Remove public from your function, there is no need for it. Change
public function AdjustCurrentExp(adjExp: int)
to
function AdjustCurrentExp(adjExp : int)
Aside from that, I sincerely suggest sticking to C#. It is a much more uniform and proper language than UnityScript and will serve you much better in the long run.
Your answer
Follow this Question
Related Questions
How to make this line work in C#? 1 Answer
JS to C# conversion is a complete disaster 1 Answer
Key for GUI.Button 2 Answers
Trouble converting player controller from JS to C# 1 Answer
Script Convert 1 Answer