- Home /
need help converting JS to C
I just purchased a script from online and i didn't realize it was in JS. Any one willing to translate this for me into C#? I've tried but keep on failing :(
var Point : float; private var GetHitEffect : float; public var targY : float; private var PointPosition : Vector3;
var PointSkin : GUISkin; var PointSkinShadow : GUISkin;
function Start() { Point = Mathf.Round(Random.Range(Point/2,Point*2)); PointPosition = transform.position + Vector3(Random.Range(-1,1),0,Random.Range(-1,1)); targY = Screen.height /2; }
function OnGUI() { var screenPos2 : Vector3 = Camera.main.camera.WorldToScreenPoint (PointPosition); GetHitEffect += Time.deltaTime*30; GUI.color = new Color (1.0f,1.0f,1.0f,1.0f - (GetHitEffect - 50) / 7); GUI.skin = PointSkinShadow; GUI.Label (Rect (screenPos2.x+8 , targY-2, 80, 70), "+" + Point.ToString()); GUI.skin = PointSkin; GUI.Label (Rect (screenPos2.x+10 , targY, 120, 120), "+" + Point.ToString()); }
function Update() { targY -= Time.deltaTime*200; }
For future reference, Here's some links I found useful in converting between C# and JS :
http://answers.unity3d.com/questions/12911/what-are-the-syntax-differences-in-c-and-javascrip.html
it is possible to use both languages, but the way they compile means that they only work one way i.e. C# gets compiled before JS, JS can see C# but C# cannot see JS :
http://docs.unity3d.com/Documentation/ScriptReference/index.Script_compilation_28Advanced29.html => point 3
http://answers.unity3d.com/questions/208383/referencing-a-c-component-script-from-js-script.html
http://answers.unity3d.com/questions/243112/calling-c-classes-from-js.html
Excuse me if I'm confused but did you just post code that you have to pay for in a public place?
$$anonymous$$y apologies, it was from the asset server for free. Also it's not the complete code just the part that made no complete sense in JS or C#.
Oh it's okay, I realized it wasn't $1,000,000 code and didn't mean to sound rude if I did. I guess I was just curious. xD
Answer by ScroodgeM · Jul 22, 2012 at 06:32 PM
using UnityEngine; public class SomeClass : MonoBehaviour { public float Point; private float GetHitEffect; public float targY; private Vector3 PointPosition;
public GUISkin PointSkin;
public GUISkin PointSkinShadow;
void Start()
{
Point = Mathf.Round(Random.Range(Point / 2, Point * 2));
PointPosition = transform.position + new Vector3(Random.Range(-1, 1), 0, Random.Range(-1, 1));
targY = Screen.height / 2;
}
void OnGUI()
{
Vector3 screenPos2 = Camera.main.camera.WorldToScreenPoint(PointPosition);
GetHitEffect += Time.deltaTime * 30;
GUI.color = new Color(1.0f, 1.0f, 1.0f, 1.0f - (GetHitEffect - 50) / 7);
GUI.skin = PointSkinShadow;
GUI.Label(new Rect(screenPos2.x + 8, targY - 2, 80, 70), "+" + Point.ToString());
GUI.skin = PointSkin;
GUI.Label(new Rect(screenPos2.x + 10, targY, 120, 120), "+" + Point.ToString());
}
void Update()
{
targY -= Time.deltaTime * 200;
}
}
thanks a lot guys! I'll give this an shot and let you know the outcome. :)
Answer by aldonaletto · Jul 22, 2012 at 06:54 PM
Basically, in C# you needed to place a new keyword before struct creators like Vector3(...) and Rect(...):
public float Point; private float GetHitEffect; public float targY; private Vector3 PointPosition;
public GUISkin PointSkin; public GUISkin PointSkinShadow ;
void Start() { Point = Mathf.Round(Random.Range(Point/2,Point*2)); // C# requires the "new" keyword before any structure PointPosition = transform.position + new Vector3(Random.Range(-1,1),0,Random.Range(-1,1)); targY = Screen.height /2; }
void OnGUI() { // Camera.main IS a camera reference! No need for .camera Vector3 screenPos2 = Camera.main.WorldToScreenPoint (PointPosition); // this instruction makes no sense in OnGUI! // maybe it should be moved to Update GetHitEffect += Time.deltaTime*30; GUI.color = new Color (1.0f,1.0f,1.0f,1.0f - (GetHitEffect - 50) / 7); GUI.skin = PointSkinShadow; // Rect also needs a "new" keyword in C# GUI.Label (new Rect (screenPos2.x+8 , targY-2, 80, 70), "+" + Point.ToString()); GUI.skin = PointSkin; GUI.Label (new Rect (screenPos2.x+10 , targY, 120, 120), "+" + Point.ToString()); }
void Update() { targY -= Time.deltaTime*200; } NOTE: As said in the comments, there's a weird instruction in OnGUI that adds Time.deltaTime*30 to GetHitEffect - this makes no sense, and will increase GetHitEffect in an unpredictable speed because OnGUI is called 2 to 4 times between frames. Maybe moving this instruction in Update could at least make it work predictably.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Converting script to C# 1 Answer
problem with converting js to c# 2 Answers
Converting JavaScript to C# 2 Answers