【发布时间】:2016-05-23 16:15:06
【问题描述】:
我正在尝试在统一 3D 中从 javascript 访问 C# 静态变量。我如何实现它?我想得到分数让我在 javascript 中使用它。
Score.cs
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Score : MonoBehaviour {
public Text txt;
public static int score;
public GameObject gameobj;
void Start () {
txt = gameobj.GetComponent<Text>();
txt.text = "Score : " + score;
}
void Update () {
txt = gameobj.GetComponent<Text> ();
txt.text = "Score : " + score;
score = score + 1;
}
int GetScore(){
return score;
}
}
Collide.js
#pragma strict
function OnTriggerEnter(otherObj: Collider){
var scoreScript : Score;
scoreScript = GetComponent("Score");
int score = scoreScript.GetScore();
if (otherObj.tag == "Wall"){
Debug.Log(score);
Application.LoadLevel(2);
}
else if (otherObj.tag == "End"){
Application.LoadLevel(6);
}
}
【问题讨论】:
-
帮自己一个忙,不要混用语言。选择 C# 和 Score.score。
标签: javascript c# variables unity3d static