-1

我正在尝试制作一个关卡完整屏幕,当我打开 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;
}
}
4

1 回答 1

1

If you want your endScreenTime to display the timerText when you activate the object, you need to use

void OnEnable()

opposed to

void Awake()

Refer to the documentation here

"Awake is called when the script instance is being loaded."

"OnEnable is called when the object becomes enabled and active."

于 2021-09-17T22:04:26.193 回答