Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Miziziziz · Jul 15, 2013 at 08:15 PM · c#javascriptguiconvert

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

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image perchik · Jul 15, 2013 at 09:20 PM 0
Share

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.

avatar image perchik · Jul 15, 2013 at 09:24 PM 0
Share

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

avatar image Miziziziz · Jul 16, 2013 at 02:01 AM 0
Share

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!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges