简体   繁体   English

按下按钮重新启动脚本 C# + Unity

[英]Restart a script on button press C# + Unity

I have a gameobject called glow and I have attached a script to it spiralclock.我有一个名为 gloom 的游戏对象,并且我在其上附加了一个脚本螺旋时钟。

I want this script to restart again from initial on button press.我希望此脚本在按下按钮时从初始位置重新启动

However, nothing is happening.然而,什么都没有发生。 I checked using Debug.Log() and the button is getting pressed.我使用 Debug.Log() 检查并按下按钮。

I have used this line in the code,我在代码中使用了这一行,

GameObject.Find("glow").GetComponent<spiralclock>().enabled = false;
GameObject.Find("glow").GetComponent<spiralclock>().enabled = true;

with OnButtonPressed() and OnButtonReleased() functions.使用 OnButtonPressed() 和 OnButtonReleased() 函数。

A code like this像这样的代码

SceneManager.LoadScene(SceneManager.GetActiveScene().name); // loads current scene

is also not working to restart the scene from start.也无法从头开始重新启动场景。

If by the definition of button, you're using Unity's UI button, you could make a method like:如果根据按钮的定义,您正在使用 Unity 的 UI 按钮,您可以创建如下方法:

public void ButtonNameClick()
{
  // your restart logic goes here
}

You could then subscribe to this method(aka - to make this method execute on a button press) either through Editor, via OnClick() or through code.然后,您可以通过编辑器、通过 OnClick() 或通过代码订阅此方法(又名 - 使此方法在按下按钮时执行)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM