简体   繁体   English

重启按钮在Unity 5.6上不起作用

[英]Restart button doesn't work on Unity 5.6

I'm trying to make a restart button on top of a game over screen. 我正试图通过屏幕在游戏顶部重新启动按钮。 But it seems that the button doesn't work. 但似乎按钮不起作用。 I've put it on the On Click() menu and it kept doing nothing when i clicked on it. 我把它放在On Click()菜单上,当我点击它时它什么都不做。 My code: 我的代码:

using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;

public class ButtonCodes:MonoBehaviour
{
    public void RestartGame()
    {
       SceneManager.LoadScene(0);
    }
}

Look into 4.6 UI Buttons. 查看4.6 UI按钮。

Create the button in your scene, and in OnClick, drag the gameObject with your ButtonCodes into the target field (left), and choose RestartGame in the right, which will be an option under ButtonCodes . 在场景中创建按钮,并在的OnClick,拖动你的游戏对象ButtonCodes到目标领域(左),并选择RestartGame在正确的,这将是下的选项ButtonCodes

This might be what you've already done.. if that's the case, try using some Debug.Log("Button was Clicked"); 这可能就是你已经完成的事情..如果是这种情况,请尝试使用一些Debug.Log("Button was Clicked"); statements. 声明。 If these show up, it's likely you haven't set up the Build settings to include any levels. 如果出现这些,可能是您没有设置Build设置以包含任何级别。 (Unity does not add levels here just by pressing play - you must do this yourself) (Unity不会通过按下游戏来增加关卡 - 你必须自己动手)

Assumming there are no errors, the most likely reason it's not working is that you didn't specify any scenes in File/Build Settings (Ctrl+Shift+B) 假设没有错误,最不可能的原因是你没有在文件/构建设置中指定任何场景(Ctrl + Shift + B)

https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html https://docs.unity3d.com/ScriptReference/SceneManagement.SceneManager.LoadScene.html

That shows that you need to order your scenes in the Build Settings if you use the sceneBuildIndex. 这表明如果使用sceneBuildIndex,则需要在Build Settings中对场景进行排序。 You could also use the second LoadScene that uses sceneName which may be more reliable if you constantly reorder scenes in Build Settings. 您还可以使用第二个使用sceneName的LoadScene,如果您不断对“构建设置”中的场景重新排序,则可能更可靠。

Also, if you haven't already, try using Debug.Log to make sure that that part of the code is actually being called. 此外,如果您还没有,请尝试使用Debug.Log来确保实际调用该部分代码。

try to put the name of the scene in the scenemanager and make sure your scene you gonna load is on your build settings 尝试将场景的名称放在场景管理器中,并确保您要加载的场景在您的构建设置上

 public void RestartGame()
{
   SceneManager.LoadScene(//name of your scene);
}

1- Sometimes when i duplicate a button and use it with other functions, it doesn't work. 1-有时当我复制一个按钮并将其与其他功能一起使用时,它不起作用。

2- Another reason that i faced was when i use inherited class, it doesn't work many times too. 2-我面临的另一个原因是当我使用继承类时,它也不会工作很多次。

3- Sometimes you forget to add your "0" scene in to build setting as well. 3-有时您忘记将“0”场景添加到构建设置中。

Thank you all for answering. 谢谢大家回答。 All of the things that you said is already been done and still, the button won't work. 你所说的所有事情都已经完成了,按钮也不起作用。 In the end, i solved the problem myself. 最后,我自己解决了这个问题。 I realized that my Canvas doesn't have an EventSystem that was an important thing for a button to work. 我意识到我的Canvas没有一个EventSystem ,这对按钮来说很重要。

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

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