【问题标题】:How to set UI Text To A Different UI Text in Unity如何在 Unity 中将 UI 文本设置为不同的 UI 文本
【发布时间】:2021-11-12 17:05:50
【问题描述】:

我正在尝试制作一个关卡完成屏幕,当我打开 EndScreenScript 时会显示你的关卡完成时间。

定时器脚本:

public class TimerScript : MonoBehaviour{

public Text timerText;
private float startTime;


void Start() {
    startTime = Time.time;
}

public void Update(){
    float t = Time.time - startTime;

    string minutes = ((int)t / 60).ToString();
    string seconds = (t % 60).ToString("f2");

    timerText.text = minutes + ":" + seconds; 
}
}

这是我的结束屏幕时间代码:

public class EndScreenScript : MonoBehaviour{

public Text endScreenTime;
public TimerScript TimerScript;

void Awake()  {
    endScreenTime = TimerScript.timerText;
}
}

【问题讨论】:

  • 究竟是什么不起作用?您能否指定您想要的行为与实际行为是什么?
  • @Armin 我想要 endScreenTime 在我激活 endScreenTime 时显示 TimerScript.timerText

标签: c# unity3d game-engine


【解决方案1】:

如果你想让你的endScreenTime在你激活对象时显示timerText,你需要使用

void OnEnable()

反对

void Awake()

请参阅此处的documentation

“在加载脚本实例时调用 Awake。”

“当对象启用并处于活动状态时调用 OnEnable。”

【讨论】:

    猜你喜欢
    • 2019-02-11
    • 1970-01-01
    • 1970-01-01
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-09
    • 1970-01-01
    相关资源
    最近更新 更多