C# Component()方法在脚本附加到的对象上搜索(在您的示例中)文本组件
希望这有帮助!C# Component()方法在脚本附加到的对象上搜索(在您的示例中)文本组件,c#,unity3d,C#,Unity3d,希望这有帮助! (哦,别忘了从您放置脚本的另一个对象中删除脚本,以防出现问题。否则您仍然会出错)no\u lives=GetComponent()似乎不起作用您确定Start()在Update之前被调用,并且GetComponent实际上正在将值设置为no\u lives?另外,lifes\u n需要转换为字符串lifes\u n.ToString()+“x”no\u lifes=GetComponent()似乎不起作用您确定在Update之前调用了Start(),并且GetComponent实
(哦,别忘了从您放置脚本的另一个对象中删除脚本,以防出现问题。否则您仍然会出错)
no\u lives=GetComponent()
似乎不起作用您确定Start()
在Update
之前被调用,并且GetComponent实际上正在将值设置为no\u lives
?另外,lifes\u n
需要转换为字符串lifes\u n.ToString()+“x”
no\u lifes=GetComponent()
似乎不起作用您确定在Update
之前调用了Start()
,并且GetComponent实际上正在将值设置为no\u lifes
?此外,还需要将lifes\n
转换为字符串lifes\n.ToString()+“x”
文本和脚本都附加到同一对象,文本和脚本都附加到同一对象
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";
}
}
void Update () {
int lives_n = gamemanager.lives_f ();
Debug.Log (lives_n);
if(no_lives != null) {
no_lives.text = string.Format("{0} x", lives_n);
}
}
no_lives = GameObject.Find("MyText").GetComponent<Text>()