Search Unity

covert code from java to c#

Discussion in 'Scripting' started by karammaks, Jul 26, 2014.

  1. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    Hi guys
    i have a code which is written in JavaScript , and i want to convert it to C#


    Code (JavaScript):
    1. class AllSpawn {
    2. var spawnManger : SpawnManager;
    3. var pointsToAdd : int = 1;
    4.     var scoreToActivate : int = 10;
    5.     var spawn : GameObject;
    6.     var spawnActive : boolean = false;
    7. }
    8.  
    9. var allSpawns : AllSpawn[];
    10. var score : int;
    11.  
    12. //automatically deactivate all spawns
    13. function Start(){
    14.     for (var s : int = 0; s < allSpawns.Length; s++){
    15.         allSpawns[s].spawnActive = false;
    16.         allSpawns[s].spawn.SetActive(false);
    17.     }
    18. }
    19.  
    20.  
    21. function AddScore(points : int){
    22.     score += points;
    23.     CheckScore();
    24.    
    25. }  
    26.  
    27. //every time when you update score, you must call this function
    28. function CheckScore(){
    29.     for( var i : int = 0; i < allSpawns.Length; i++){
    30.         if( score >= allSpawns[i].scoreToActivate && allSpawns[i].spawnActive == false){
    31.             allSpawns[i].spawnActive = true;
    32.             allSpawns[i].spawn.SetActive(true);
    33.         }
    34.     }
    35. }
    36.  
    37.  
    38.  
    39.  
    if there is any website or program to convert , please tell me
     
  2. renman3000

    renman3000

    Joined:
    Nov 7, 2011
    Posts:
    6,697
    There is. Google it.
     
  3. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    i tried and find only 1 website which now undermantinance S:
     
  4. MD_Reptile

    MD_Reptile

    Joined:
    Jan 19, 2012
    Posts:
    2,664
    As produced by the M2H site here: http://www.m2h.nl/files/js_to_c.php

    Will probably require tweaking.

    Code (CSharp):
    1. // Converted from UnityScript to C# at http://www.M2H.nl/files/js_to_c.php - by Mike Hergaarden
    2. // Do test the code! You usually need to change a few small bits.
    3.  
    4. using UnityEngine;
    5. using System.Collections;
    6.  
    7. public class MYCLASSNAME : MonoBehaviour {
    8. class AllSpawn {
    9. SpawnManager spawnManger;
    10. int pointsToAdd = 1;
    11.     int scoreToActivate = 10;
    12.     GameObject spawn;
    13.     bool  spawnActive = false;
    14. }
    15. AllSpawn[] allSpawns;
    16. int score;
    17. //automatically deactivate all spawns
    18. void  Start (){
    19.     for (int s = 0; s < allSpawns.Length; s++){
    20.         allSpawns[s].spawnActive = false;
    21.         allSpawns[s].spawn.SetActive(false);
    22.     }
    23. }
    24. void  AddScore ( int points  ){
    25.     score += points;
    26.     CheckScore();
    27.  
    28. }
    29. //every time when you update score, you must call this function
    30. void  CheckScore (){
    31.     for( int i = 0; i < allSpawns.Length; i++){
    32.         if( score >= allSpawns[i].scoreToActivate && allSpawns[i].spawnActive == false){
    33.             allSpawns[i].spawnActive = true;
    34.             allSpawns[i].spawn.SetActive(true);
    35.         }
    36.     }
    37. }
    38. }
     
  5. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    Thank you very much :) , but i have a problem , the JavaScript code shows the variables in the inspector "check the picture in the attachment" , but the C# code doesn't show those variables , i'ved tried to change some lines of the codes and identify them as public variables, but still do not show on the inspector "Check my modifation to the C# converted code">>
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SpawnManager : MonoBehaviour {
    5.  
    6.         public class AllSpawn {
    7.         public int scoreToActivate = 10;
    8.             public GameObject spawn;
    9.  
    10.             public bool spawnActive = false;
    11.         }
    12.         AllSpawn[] allSpawns;
    13.         int score;
    14.         //automatically deactivate all spawns
    15.         void  Start (){
    16.             for (int s = 0; s < allSpawns.Length; s++){
    17.                 allSpawns[s].spawnActive= false;
    18.                 allSpawns[s].spawn.SetActive(false);
    19.             }
    20.         }
    21.         public void  AddScore ( int points  ){
    22.             score += points;
    23.             CheckScore();
    24.            
    25.         }
    26.         //every time when you update score, you must call this function
    27.         void  CheckScore (){
    28.             for( int i = 0; i < allSpawns.Length; i++){
    29.                 if( score >= allSpawns[i].scoreToActivate && allSpawns[i].spawnActive == false){
    30.                     allSpawns[i].spawnActive = true;
    31.                     allSpawns[i].spawn.SetActive(true);
    32.                 }
    33.             }
    34.         }
    35.     }
    36.  
    37.  
     

    Attached Files:

  6. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    Stick the keyword 'public' in front of "AllSpawn[] allSpawns" to reveal it in the inspector.

    In UnityScript variables are public by default.
    In C# variables are private by default.

    Private variables are hidden in the inspector and are not serialized by default.

    I hope that this helps!
     
  7. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    i did it now , but still hidden or not appear in the inspector :S
     
  8. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    Looking closer at your code above it seems that you are using a vanilla C# class called "AllSpawn". This class needs to be decorated with an attribute so that it can be serialized by the Unity serializer:

    In your code, update the following:
    Code (csharp):
    1. public class AllSpawn {
    to:
    Code (csharp):
    1. [System.Serializable]
    2. public class AllSpawn {
    I would suggest placing each class within its own file to make your code easier to maintain. Or at least applying proper indentation so that classes are easier to spot.

    I hope this helps!
     
  9. karammaks

    karammaks

    Joined:
    Apr 6, 2014
    Posts:
    146
    Thank you very much ! , now they are appear , but i dont know why the spawners dont work if i put different amounts for score to activate them , they were working when the code was in Java :confused: