Unity 5.3.2 - 无法在检查器中使用 UI 文本对象
Unity 5.3.2 - Can't use UI Text object in inspector
所以我昨晚升级到 Unity 5.3.2,现在我不能在检查器中将 UI 文本对象拖到脚本中。 (下图)
除运行时外,没有编译器错误。我每次都会收到此错误:
NullReferenceException: Object reference not set to an instance of an object
Scoring.updateScores () (at Assets/Scripts/Scoring.js:21)
我有下面的 javascript 代码。评论是我试过的东西。请询问并需要澄清。
#pragma strict
var score : int;
//var scoreScore : GameObject;
//var highScore : GameObject;
var scoreScore : UnityEngine.UI.Text;
var highScore : UnityEngine.UI.Text;
function Start () {
//scoreScore = GameObject.Find("scoreScore").GetComponent.<UnityEngine.UI.Text>();
//highScore = GameObject.Find("highScore").GetComponent.<UnityEngine.UI.Text>();
score = 0;
updateScores();
}
function updateScores() {
if (score >= PlayerPrefs.GetInt("highScore")) {
PlayerPrefs.SetInt("highScore", score);
}
scoreScore.text = "" + score.ToString();
highScore.text = "" + PlayerPrefs.GetInt("highScore");
}
当您在检查器中查看 脚本 时(通过在“资产”文件夹中单击它),您可以为该脚本设置默认对象。这些对象只能来自您的 Sssets——它不允许您从某个场景中拖动对象。您只能将场景中的对象拖动到该场景中的脚本实例(例如,附加到特定游戏对象的计分脚本)。
尝试创建一个 GameObject,将 Scoring 脚本附加到它,在检查器中选择 GameObject,然后将 UIText 元素拖动到您附加到该脚本的实例游戏对象。
将这些文本对象拖放到您的项目中,使它们成为预制件。然后你可以分配它们。
所以我昨晚升级到 Unity 5.3.2,现在我不能在检查器中将 UI 文本对象拖到脚本中。 (下图)
除运行时外,没有编译器错误。我每次都会收到此错误:
NullReferenceException: Object reference not set to an instance of an object
Scoring.updateScores () (at Assets/Scripts/Scoring.js:21)
我有下面的 javascript 代码。评论是我试过的东西。请询问并需要澄清。
#pragma strict
var score : int;
//var scoreScore : GameObject;
//var highScore : GameObject;
var scoreScore : UnityEngine.UI.Text;
var highScore : UnityEngine.UI.Text;
function Start () {
//scoreScore = GameObject.Find("scoreScore").GetComponent.<UnityEngine.UI.Text>();
//highScore = GameObject.Find("highScore").GetComponent.<UnityEngine.UI.Text>();
score = 0;
updateScores();
}
function updateScores() {
if (score >= PlayerPrefs.GetInt("highScore")) {
PlayerPrefs.SetInt("highScore", score);
}
scoreScore.text = "" + score.ToString();
highScore.text = "" + PlayerPrefs.GetInt("highScore");
}
当您在检查器中查看 脚本 时(通过在“资产”文件夹中单击它),您可以为该脚本设置默认对象。这些对象只能来自您的 Sssets——它不允许您从某个场景中拖动对象。您只能将场景中的对象拖动到该场景中的脚本实例(例如,附加到特定游戏对象的计分脚本)。
尝试创建一个 GameObject,将 Scoring 脚本附加到它,在检查器中选择 GameObject,然后将 UIText 元素拖动到您附加到该脚本的实例游戏对象。
将这些文本对象拖放到您的项目中,使它们成为预制件。然后你可以分配它们。