- Home /
Converting JS to C#
How would I convert
var status : GameObject;
var curStatus = "";
function OnTriggerEnter(other : Collider) {
status.GetComponent(TextMesh).text = curStatus.ToString();
Drivetrain.maxRPM = 8000;
}
into a C# script? Or is there a way to define a C# script from a JS script.,
Answer by clunk47 · Jan 01, 2013 at 10:22 PM
You would need to define a class in the script, and the script name needs to be the same as your class. Let's say for example's sake, we'll have a C# script named EXAMPLE. You'll notice where I put "using UnityEngine and ...System.Collections... This is required. There are others like System.IO, System.Collections.Generic, and many more. These 2 are all we need for this specific script. You won't need to use ToString() since we already define curStatus as a string. You only need to use ToString() if you, for example have an int like health = 10; We would use health.ToString() to display on GUI or Debug. You'll probably wonder why I used "public" on GameObject and string. This is because in C#, variables are private by default, unlike JS where they are public by default, which allows them to show in the inspector. The rest of the differences should be self explanatory.
using UnityEngine;
using System.Collections;
public class EXAMPLE : MonoBehaviour
{
public GameObject status;
public string curStatus = "";
void OnTriggerEnter(Collider other)
{
status.GetComponent<TextMesh>().text = curStatus;
Drivetrain.maxRPM = 8000;
}
}
Answer by Piflik · Jan 01, 2013 at 10:21 PM
C# needs variable types. Also some little differences in syntax and needs enclosing stuff:
using UnityEngine;
using System.Collections;
public class Status : MonoBehaviour { //scriptname needs to match the name of the class
public GameObject status;
public string curStatus = "";
void OnTriggerEnter(Collider other) {
status.GetComponent<TextMesh>().text = curStatus;
Drivetrain.maxRPM = 8000;
}
}
Answer by doublethink · Jan 16, 2013 at 11:01 PM
Try using this handy Javascript to C# converter http://files.m2h.nl/js_to_c.php
Your answer
Follow this Question
Related Questions
how can convert this in js? 1 Answer
Trying to Convert this JS to C# 2 Answers
Js to C# convert 1 Answer
Lightning Script Js To C# Conversion errors 2 Answers
Multiple Cars not working 1 Answer