Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

convert c# to javascript

Discussion in 'Getting Started' started by joytdecastro, Feb 26, 2015.

  1. joytdecastro

    joytdecastro

    Joined:
    Feb 15, 2015
    Posts:
    21
    hi guys, can you help me to convert this to Javasript?? coz i don't know how to get the score from different script.. thanks in advance..

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameControlScript : MonoBehaviour {
    5.    
    6.     public GUISkin skin;
    7.     float timeRemaining = 500;
    8.     float timeExtension = 3f;
    9.     float timeDeduction = 2f;
    10.     float totalTimeElapsed = 0;
    11.     float score=0f;
    12.     public bool isGameOver = false;
    13.    
    14.     void Start(){
    15.         Time.timeScale = 1;  // set the time scale to 1, to start the game world. This is needed if you restart the game from the game over menu
    16.     }
    17.    
    18.     void Update () {
    19.         if(isGameOver)
    20.             return;
    21.        
    22.         totalTimeElapsed += Time.deltaTime;
    23.         score = totalTimeElapsed*100;
    24.         timeRemaining -= Time.deltaTime;
    25.         if(timeRemaining <= 0){
    26.             isGameOver = true;
    27.         }
    28.     }
    29.    
    30.     public void PowerupCollected()
    31.     {
    32.         timeRemaining += timeExtension;
    33.     }
    34.    
    35.     public void AlcoholCollected()
    36.     {
    37.         timeRemaining -= timeDeduction;
    38.     }
    39.    
    40.     void OnGUI()
    41.     {
    42.         GUI.skin=skin; //use the skin in game over menu
    43.         //check if game is not over, if so, display the score and the time left
    44.         if(!isGameOver)  
    45.         {
    46.             //GUI.Label(new Rect(10, 10, Screen.width/5, Screen.height/6),"TIME LEFT: "+((int)timeRemaining).ToString());
    47.             GUI.Label(new Rect(Screen.width-(Screen.width/10), 10, Screen.width/10, Screen.height/10), "SCORE: "+((int)score).ToString());
    48.         }
    49.         //if game over, display game over menu with score
    50.         else
    51.         {
    52.             Time.timeScale = 0; //set the timescale to zero so as to stop the game world
    53.            
    54.             //display the final score
    55.             GUI.Box(new Rect(Screen.width/4, Screen.height/4, Screen.width/2, Screen.height/2), "GAME OVER\nYOUR SCORE: "+(int)score);
    56.            
    57.             //restart the game on click
    58.             if (GUI.Button(new Rect(Screen.width/4+10, Screen.height/4+Screen.height/10+10, Screen.width/2-20, Screen.height/10), "RESTART")){
    59.                 Application.LoadLevel(Application.loadedLevel);
    60.             }
    61.            
    62.             //load the main menu, which as of now has not been created
    63.             if (GUI.Button(new Rect(Screen.width/4+10, Screen.height/4+2*Screen.height/10+10, Screen.width/2-20, Screen.height/10), "MAIN MENU")){
    64.                 Application.LoadLevel(0);
    65.             }
    66.            
    67.             //exit the game
    68.             if (GUI.Button(new Rect(Screen.width/4+10, Screen.height/4+3*Screen.height/10+10, Screen.width/2-20, Screen.height/10), "EXIT GAME")){
    69.                 Application.Quit();
    70.             }
    71.         }
    72.     }
    73. }
     
  2. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,446
  3. joytdecastro

    joytdecastro

    Joined:
    Feb 15, 2015
    Posts:
    21
  4. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    What is the line of C# code ?
    What is your line of UnityJs code ?

    What do you expect the parameters should be for UnityEngine.GUI.Label ?
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Learn this first in whatever language you prefer. You won't get far without understanding the mysteries of GetComponent.

    This script is so trivial that you would be better off writing it from scratch in your preferred language. There is also no reason to use OnGUI anymore. Why not do yourself a favour and switch to the latest UI tools?
     
  6. jshrek

    jshrek

    Joined:
    Mar 30, 2013
    Posts:
    220