Hi guys i have a code which is written in JavaScript , and i want to convert it to C# Code (JavaScript): class AllSpawn { var spawnManger : SpawnManager; var pointsToAdd : int = 1; var scoreToActivate : int = 10; var spawn : GameObject; var spawnActive : boolean = false; } var allSpawns : AllSpawn[]; var score : int; //automatically deactivate all spawns function Start(){ for (var s : int = 0; s < allSpawns.Length; s++){ allSpawns[s].spawnActive = false; allSpawns[s].spawn.SetActive(false); } } function AddScore(points : int){ score += points; CheckScore(); } //every time when you update score, you must call this function function CheckScore(){ for( var i : int = 0; i < allSpawns.Length; i++){ if( score >= allSpawns[i].scoreToActivate && allSpawns[i].spawnActive == false){ allSpawns[i].spawnActive = true; allSpawns[i].spawn.SetActive(true); } } } if there is any website or program to convert , please tell me
As produced by the M2H site here: http://www.m2h.nl/files/js_to_c.php Will probably require tweaking. Code (CSharp): // Converted from UnityScript to C# at http://www.M2H.nl/files/js_to_c.php - by Mike Hergaarden // Do test the code! You usually need to change a few small bits. using UnityEngine; using System.Collections; public class MYCLASSNAME : MonoBehaviour { class AllSpawn { SpawnManager spawnManger; int pointsToAdd = 1; int scoreToActivate = 10; GameObject spawn; bool spawnActive = false; } AllSpawn[] allSpawns; int score; //automatically deactivate all spawns void Start (){ for (int s = 0; s < allSpawns.Length; s++){ allSpawns[s].spawnActive = false; allSpawns[s].spawn.SetActive(false); } } void AddScore ( int points ){ score += points; CheckScore(); } //every time when you update score, you must call this function void CheckScore (){ for( int i = 0; i < allSpawns.Length; i++){ if( score >= allSpawns[i].scoreToActivate && allSpawns[i].spawnActive == false){ allSpawns[i].spawnActive = true; allSpawns[i].spawn.SetActive(true); } } } }
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): using UnityEngine; using System.Collections; public class SpawnManager : MonoBehaviour { public class AllSpawn { public int scoreToActivate = 10; public GameObject spawn; public bool spawnActive = false; } AllSpawn[] allSpawns; int score; //automatically deactivate all spawns void Start (){ for (int s = 0; s < allSpawns.Length; s++){ allSpawns[s].spawnActive= false; allSpawns[s].spawn.SetActive(false); } } public void AddScore ( int points ){ score += points; CheckScore(); } //every time when you update score, you must call this function void CheckScore (){ for( int i = 0; i < allSpawns.Length; i++){ if( score >= allSpawns[i].scoreToActivate && allSpawns[i].spawnActive == false){ allSpawns[i].spawnActive = true; allSpawns[i].spawn.SetActive(true); } } } }
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!
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): public class AllSpawn { to: Code (csharp): [System.Serializable] 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!
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