我有以下一段代碼。我將編輯器中的文本組件引用爲no_lives。遊戲管理者(單身人士)正在實例化之前的一個場景。 debug.log()在控制檯中顯示5。但是,當我嘗試設置文本時,我發現引用未設置爲對象的實例。這是爲什麼?Unity3D C# - 文本UI
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class level1_script : MonoBehaviour {
public Text no_lives;
// Use this for initialization
void Start() {
no_lives = GetComponent<Text>();
}
// Update is called once per frame
void Update() {
int lives_n = gamemanager.lives_f();
Debug.Log (lives_n);
no_lives.text = lives_n + " x";
}
}
'no_lives = GetComponent()'似乎不是工作 –
Dawnkeeper
你確定'Start()'在Update之前被調用,並且GetComponent實際上是將一個值設置爲'no_lives'?另外'lives_n'需要轉換爲字符串'lives_n.ToString()+「x」' – Nkosi