Quantcast
Channel: Latest Questions on Unity Answers
Viewing all articles
Browse latest Browse all 171066

translated script from C# to JS; GUI not working properly?

$
0
0
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); }

Viewing all articles
Browse latest Browse all 171066

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>