Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Convert Java to C#

Discussion in 'Scripting' started by KnightRiderGuy, Nov 29, 2015.

  1. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    515
    Can someone convert this Java to C# please?

    Code (JavaScript):
    1. #pragma strict
    2. var Alarm : AudioClip ;
    3. var timer: float = 3600;
    4. var isFinishedTimer : boolean = true;
    5. public var displayText : UnityEngine.UI.Text;
    6. public var timeText : UnityEngine.UI.Text;
    7. var minsDisplay : String;
    8. var secsDisplay : String;
    9. var mySeconds : int = 0;
    10. private var oldTimer : float;
    11. //Random Clips
    12. var voices : AudioClip[];
    13. /*//Begin New
    14. function Awake ()
    15. {
    16.     // Make LevelData a singleton, so we keep ourself alive and kill any clones
    17.     if ( FindObjectsOfType(CountdownTimerManager).Length != 1 )     // If one of me already exists I must be a clone, so destroy me (don't feel bad about it)
    18.         Destroy (gameObject);
    19.     else        // I'm the original, so keep me around
    20.         DontDestroyOnLoad (gameObject);
    21. }
    22. //End New
    23. /*function Awake () {
    24.          DontDestroyOnLoad (transform.gameObject);
    25.      }*/
    26. function Start(){
    27.      
    28.      oldTimer = timer;
    29. }
    30. function Update(){
    31.  
    32.      if (!isFinishedTimer) {
    33.          timer -= Time.deltaTime;
    34.      }
    35.    
    36.      CurrentTime();
    37. }
    38. function CurrentTime() {
    39.      var dt : System.DateTime = System.DateTime.Now;
    40.      var h : int = dt.Hour;
    41.      var m : int = dt.Minute;
    42.      var s : int = dt.Second;
    43.      timeText.text = h + ":" + m + ":" + s;
    44.    
    45.      if(mySeconds != s)
    46.      {
    47.          mySeconds = s;
    48.          Timing();
    49.      }
    50.    
    51. }
    52. function Timing()
    53. {
    54.      if (timer > 0) {
    55.          //var minsDisplay : String = parseInt( timer / 60 ).ToString();
    56.          minsDisplay = parseInt( timer / 60 ).ToString();
    57.        
    58.          //var secsDisplay : String = parseInt( timer ).ToString();
    59.          secsDisplay = parseInt( timer ).ToString();
    60.          
    61.          if ( (timer - ( parseInt(minsDisplay) * 60)) > 10 ) {
    62.               secsDisplay = parseInt( timer - ( parseInt(minsDisplay) * 60) ).ToString();
    63.          }
    64.          else {
    65.              secsDisplay = "0" + parseInt( timer - ( parseInt(minsDisplay) * 60) ).ToString();
    66.          }
    67.        
    68.          //displayText.text = minsDisplay + " : " + secsDisplay;
    69.      }
    70.      //Timer Reaches End We Can Do Something Here
    71.      else {
    72.           timer += oldTimer;
    73.           GetComponent.<AudioSource>().PlayOneShot(Alarm);//Plays Alarm Sound
    74.           isFinishedTimer = true;//Sets Inspector Value to true or false based on what is set here
    75.           yield WaitForSeconds (5.8);//Wait Time Setting
    76.           //Do Something if Desired
    77.          
    78.          //if (GetComponent.<AudioSource>().isPlaying) return; // don't play a new sound while the last hasn't finished
    79.            GetComponent.<AudioSource>().clip = voices[Random.Range(0,voices.length)];
    80.           GetComponent.<AudioSource>().Play();
    81.          
    82.          
    83.          
    84.           Debug.Log ("Timer Ended");
    85.      }
    86.      displayText.text = minsDisplay + " : " + secsDisplay;
    87. }
    88. //Timer Stop Button
    89. public function GoTimerStop()
    90. {
    91.      isFinishedTimer = true;
    92. }
    93. //Timer Start Button
    94. public function GoTimerStart()
    95. {
    96.      isFinishedTimer = false;
    97. }
    98. //Timer Settings
    99. public function GoTimerSetting60Sec()
    100. {
    101.      timer = 60;
    102. }
    103. public function GoTimerSetting5Min()
    104. {
    105.      timer = 300;
    106. }
    107. public function GoTimerSetting10Min()
    108. {
    109.      timer = 600;
    110. }
    111. public function GoTimerSetting20Min()
    112. {
    113.      timer = 1200;
    114. }
    115. public function GoTimerSetting30Min()
    116. {
    117.      timer = 1800;
    118. }
    119. public function GoTimerSetting40Min()
    120. {
    121.      timer = 2400;
    122. }
    123. public function GoTimerSetting50Min()
    124. {
    125.      timer = 3000;
    126. }
    127. public function GoTimerSetting1Hr()
    128. {
    129.      timer = 3600;
    130. }
    131. public function GoTimerSetting1Point5Hr()
    132. {
    133.      timer = 5400;
    134. }
    135. public function GoTimerSetting2Hr()
    136. {
    137.      timer = 7200;
    138. }
    139. public function GoTimerSetting2Point5Hr()
    140. {
    141.      timer = 9000;
    142. }
    143. public function GoTimerSetting3Hr()
    144. {
    145.      timer = 10800;
    146. }
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Typically people hear don't do your work for you. You might get lucky. But you might be better off learning it yourself or paying someone with the skills.
     
    Meltdown and apsdsm like this.
  3. passerbycmc

    passerbycmc

    Joined:
    Feb 12, 2015
    Posts:
    1,741
    also Java != JavaScript != UnityScript
     
    Meltdown likes this.
  4. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    515
    I love people willing to help. ;)

    Most of it is converted but it has bugs. The buttons for the timers in the inspector are reading OK but their functions are not being activated for some reason and the timer is not working?

    I should point out that the ONLY reason I wanted this converted is because there does not seem to be a simple way for Java (Unity Script) my bad to talk to C# which I needed in order to send a character out to the Arduino.
    Dam this stuff is a real pill sometimes... especially for a non coder like me forced to go it alone on this project.
    So that being said I REALLY appreciate the help. Any help. ;)

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5. public class CountdownTimerManager : MonoBehaviour {
    6.  
    7.     public AudioClip Alarm;
    8.     float timer  = 3600;
    9.     bool isFinishedTimer   = true;
    10.     public UnityEngine.UI.Text displayText;
    11.     public UnityEngine.UI.Text timeText;
    12.    
    13.     string minsDisplay;
    14.     string secsDisplay;
    15.    
    16.     int mySeconds  = 0;
    17.    
    18.     private float oldTimer;
    19.    
    20.     //Random Clips
    21.     public AudioClip[] voices;
    22.    
    23.    
    24.     /*//Begin New
    25.     function Awake ()
    26.     {
    27.         // Make LevelData a singleton, so we keep ourself alive and kill any clones
    28.         if ( FindObjectsOfType(CountdownTimerManager).Length != 1 )     // If one of me already exists I must be a clone, so destroy me (don't feel bad about it)
    29.             Destroy (gameObject);
    30.         else        // I'm the original, so keep me around
    31.             DontDestroyOnLoad (gameObject);
    32.     }
    33.     //End New
    34.    
    35.    
    36.     /*function Awake () {
    37.                DontDestroyOnLoad (transform.gameObject);
    38.            }*/
    39.    
    40.     void Start(){
    41.        
    42.         oldTimer = timer;
    43.     }
    44.    
    45.     void Update(){
    46.        
    47.         if (!isFinishedTimer) {
    48.             timer -= Time.deltaTime;
    49.         }
    50.        
    51.         CurrentTime();
    52.     }
    53.    
    54.     void CurrentTime() {
    55.         System.DateTime dt  = System.DateTime.Now;
    56.         int h  = dt.Hour;
    57.         int m  = dt.Minute;
    58.         int s  = dt.Second;
    59.        
    60.         timeText.text = h + ":" + m + ":" + s;
    61.        
    62.         if(mySeconds != s)
    63.         {
    64.             mySeconds = s;
    65.             Timing();
    66.         }
    67.        
    68.     }
    69.    
    70.     IEnumerator  Timing()
    71.     {
    72.         if (timer > 0) {
    73.             //var minsDisplay : String = parseInt( timer / 60 ).ToString();
    74.             minsDisplay = System.Convert.ToInt32( timer / 60 ).ToString();
    75.            
    76.             //var secsDisplay : String = parseInt( timer ).ToString();
    77.             secsDisplay = System.Convert.ToInt32( timer ).ToString();
    78.            
    79.             if ( (timer - ( System.Convert.ToInt32(minsDisplay) * 60)) > 10 ) {
    80.                 secsDisplay = System.Convert.ToInt32( timer - ( System.Convert.ToInt32(minsDisplay) * 60) ).ToString();
    81.             }
    82.             else {
    83.                 secsDisplay = "0" + System.Convert.ToInt32( timer - ( System.Convert.ToInt32(minsDisplay) * 60) ).ToString();
    84.             }
    85.            
    86.             //displayText.text = minsDisplay + " : " + secsDisplay;
    87.         }
    88.         //Timer Reaches End We Can Do Something Here
    89.         else {
    90.             timer += oldTimer;
    91.             GetComponent<AudioSource>().PlayOneShot(Alarm);//Plays Alarm Sound
    92.             isFinishedTimer = true;//Sets Inspector Value to true or false based on what is set here
    93.             yield return new WaitForSeconds(5.8f);//Wait Time Setting
    94.             //Do Something if Desired
    95.            
    96.             //if (GetComponent.<AudioSource>().isPlaying) return; // don't play a new sound while the last hasn't finished
    97.             GetComponent<AudioSource>().clip = voices[Random.Range(0,voices.Length)];
    98.             GetComponent<AudioSource>().Play();
    99.            
    100.            
    101.            
    102.             Debug.Log ("Timer Ended");
    103.         }
    104.         displayText.text = minsDisplay + " : " + secsDisplay;
    105.     }
    106.    
    107.    
    108.    
    109.     //Timer Stop Button
    110.     public void GoTimerStop()
    111.     {
    112.         isFinishedTimer = true;
    113.     }
    114.    
    115.     //Timer Start Button
    116.     public void GoTimerStart()
    117.     {
    118.         isFinishedTimer = false;
    119.     }
    120.    
    121.     //Timer Settings
    122.     public void GoTimerSetting60Sec()
    123.     {
    124.         timer = 60;
    125.     }
    126.    
    127.     public void GoTimerSetting5Min()
    128.     {
    129.         timer = 300;
    130.     }
    131.    
    132.     public void GoTimerSetting10Min()
    133.     {
    134.         timer = 600;
    135.     }
    136.    
    137.     public void GoTimerSetting20Min()
    138.     {
    139.         timer = 1200;
    140.     }
    141.    
    142.     public void GoTimerSetting30Min()
    143.     {
    144.         timer = 1800;
    145.     }
    146.    
    147.     public void GoTimerSetting40Min()
    148.     {
    149.         timer = 2400;
    150.     }
    151.    
    152.     public void GoTimerSetting50Min()
    153.     {
    154.         timer = 3000;
    155.     }
    156.    
    157.     public void GoTimerSetting1Hr()
    158.     {
    159.         timer = 3600;
    160.     }
    161.    
    162.     public void GoTimerSetting1Point5Hr()
    163.     {
    164.         timer = 5400;
    165.     }
    166.    
    167.     public void GoTimerSetting2Hr()
    168.     {
    169.         timer = 7200;
    170.     }
    171.    
    172.     public void GoTimerSetting2Point5Hr()
    173.     {
    174.         timer = 9000;
    175.     }
    176.    
    177.     public void GoTimerSetting3Hr()
    178.     {
    179.         timer = 10800;
    180.     }
    181. }
    182.  
     
  5. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    357
  6. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    515
    That might be but those auto convert pages never seem to work right.... which is probably why this is not working.
     
  7. aer0ace

    aer0ace

    Joined:
    May 11, 2012
    Posts:
    1,513
    Let me teach you how to fish.

    First of all:
    The code that you are converting from is Javascript. Not Java. This is not an anal thing like, "Don't WRITE IN CAPS". It's a technical thing. Java and Javascript are two completely different languages that will not compile if you mixed the two, since they require two different compilers.

    With that aside,

    You'll have to take one step at a time. Since you have specifically identified the buttons not working, what do they look like in the inspector? Not sure what "reading OK" means. Does that mean the CountdownTimerManager Monobehaviour is properly assigned to the button, and the public function in that monobehaviour is assigned to the click callback function? Let's start there. Help us help you.

    Here's another thing you can do. I'm going to assume that your click functions are GoTimerStart and GoTimerStop. Put a Debug.Log("some text"); in here, to verify that your button is hooked up. If it doesn't display in the console, then it's definitely not hooked up, and you have to investigate why this is.
     
    KnightRiderGuy and Kiwasi like this.
  8. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    515
    Thanks for the heads up, I guess not being a coder I never knew that I just always assumed Java and Javascripts were the same thing, I did know that Unity used Unity script that I kept hearing everyone referring to as Javascript.

    Personally these days I have been trying to use more C# in my project as I hear its better?? I don't know Just saying what I hear all the time please feel free to correct me as like I say I'm not a coder.... I muddle through as I have no choice in the matter on this project... lol amazes me I have gotten it this far ;)

    I had a kind soul on Unity Answers Fix the converted script so now it works finally. That part of my project had been a pain in the backside for better part of a year now.
     
  9. grizzly

    grizzly

    Joined:
    Dec 5, 2012
    Posts:
    357
    Works fine here. Just requires a little manual effort (as mentioned by the author); evidently more effort than you were willing to put in.
     
  10. KnightRiderGuy

    KnightRiderGuy

    Joined:
    Nov 23, 2014
    Posts:
    515
    K.I.T.T. had something to say about that but I'm not able to repeat it here lol