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.,
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;
}
}
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;
}
}