我正在构建 Unity 3D 游戏,如果用户处于第三级,我想签入脚本。如果为 true,则否则 .我尝试将这个简单的if语句实现到脚本中,但我得到了:timeLeft = 120;timeLeft = 90;
Invalid token 'if' in class, struct, or interface member declaration
如果我将语句放入 start 方法中,我得到:
The name 'timeLeft' does not exist in the current context
如何解决?
计时器.cs
public class Timer : MonoBehaviour
{
public Collide _collide;
Text instruction;
private void Start()
{
instruction = GetComponent<Text>();
InvokeRepeating("time", 0, 1);
}
if (SceneManager.GetActiveScene().buildIndex == 7)
int timeLeft = 120;
else int timeLeft = 90;
private void time()
{
int buildIndex = SceneManager.GetActiveScene();
if (_collide.obji <= 0 && timeLeft > 0)
SceneManager.LoadScene(buildIndex switch { 1 => 2, 3 => 5, 7 => 9 });
else if (_collide.obji > 0 && timeLeft <= 0)
SceneManager.LoadScene(buildIndex switch { 1 => 3, 3 => 6, 7 => 8 });
if (timeLeft > 0)
{
timeLeft -= 1;
instruction.text = (timeLeft).ToString();
}
else if (timeLeft <= 0)
instruction.text = "0";
}
}
慕娘9325324
慕盖茨4494581
相关分类