简体   繁体   English

如何使按钮出现在游戏结束时?

[英]How do I make buttons appear at the end of the game?

A few days ago I started Unity and I used the official tutorials to learn. 几天前,我启动了Unity,并使用了官方教程进行学习。 I've got the first game going and i'm starting to love programming. 我已经有了第一款游戏,而且我开始喜欢编程。 I'm very new to this and tought it would help me if I made these tutorial games better. 我对此很陌生,如果我将这些教程游戏做得更好,它将对我有帮助。 Everything went great, but now i'm stuck. 一切都很好,但现在我被困住了。 I want a menu that appears when I win the game. 我想要在我赢得比赛时出现的菜单。 I made the menu and it has 3 buttons ('Try again', 'Menu' and 'Exit'). 我做了菜单,它有3个按钮(“重试”,“菜单”和“退出”)。 Here is a picture: http://prntscr.com/9jy71h . 这是一张图片: http : //prntscr.com/9jy71h The buttons work, but my only problem is that they appear there the whole game long, while I only want them at the end. 这些按钮有效,但我唯一的问题是它们在整个游戏中都出现了很长的时间,而我只希望它们在最后。 I tried a lot, but i seem to get this error all the time: 'Retry' doesn't exist in the current context. 我尝试了很多,但似乎一直都遇到此错误: “ Retry”在当前上下文中不存在。

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class PlayerController : MonoBehaviour {
    public float speed; 
    public Text countText;
    public Text winText;    
    private Rigidbody rb;
    private int count;

    void Start ()
    // The game starts here.
    {
        rb = GetComponent<Rigidbody>();
        count = 0;
        SetCountText ();
        winText.text = "";
        Retry.GameObject.SetActive(false);
        Menu.GameObject.SetActive(false);
        Exit.GameObject.SetActive(false);
   }

   void FixedUpdate ()
   // Movement.
   {
       float moveHorizontal = Input.GetAxis ("Horizontal");
       float moveVertical = Input.GetAxis ("Vertical");

       Vector3 movement = new Vector3 (moveHorizontal, 0.0f, moveVertical);

       rb.AddForce (movement * speed);
   }
   void OnTriggerEnter(Collider other)
   // Picking things up and count score.
   {
       if (other.gameObject.CompareTag("Pick Up"))
       {
           other.gameObject.SetActive (false);
           count = count + 1;
           SetCountText ();
       }
   }

   void SetCountText ()
   {
       countText.text = "Count:" + count.ToString ();
       if (count >= 12)
       // End of the game, you won.
       {
            winText.text = "You win!";
            Retry.GameObject.SetActive(true);
            Menu.GameObject.SetActive(true);
            Exit.GameObject.SetActive(true);
       }
   }
}

Why can't he recognize my gameObject that I made? 他为什么不能识别我制作的我的游戏对象? (the buttons you saw at the picture) Sorry for my bad english and thank you for taking the time to read this and if possible answer. (您在图片上看到的按钮)对不起,我的英语不好,谢谢您抽出宝贵的时间阅读本文,并在可能的情况下回答。

EDIT: http://prntscr.com/9jybke These are all the errors. 编辑: http : //prntscr.com/9jybke这些都是错误。 If I however, delete the 6 gameobjective(bool) lines on the top and bottom it works fine, but I have the menu ON all the time. 但是,如果我删除顶部和底部的6个gameobjective(bool)行,效果很好,但是我一直都在打开菜单。

What you are missing is references from your script to the button game objects, so that you can turn them on/off. 您缺少的是脚本中对按钮游戏对象的引用,因此您可以打开/关闭它们。

If you add to your script 如果您添加到脚本

public GameObject retryButton;

And similarly for the other buttons, and then in Unity in the inspector for your script you drag the three button GameObjects to their corresponding "slots" in the inspector. 同样,对于其他按钮,然后在检查器中的Unity中,将脚本中的三个按钮GameObject拖到检查器中它们相应的“插槽”中。

Then in your code you replace Retry.GameObject.SetActive() with retryButton.SetActive() 然后在您的代码中,用Retry.GameObject.SetActive()替换retryButton.SetActive()

And likewise for the other buttons. 其他按钮也一样。

Actually, it seems you are already doing this for your "countText" and "winText". 实际上,看来您已经为“ countText”和“ winText”执行了此操作。

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

相关问题 如何使菜单出现或消失? - How do I make a menu appear or disppear? 如何使“ TreeListControl”显示为“ TreeView”? - How do I make “TreeListControl” appear as a “TreeView”? 加载ProgressBar后如何使按钮出现? - How to make buttons appear after ProgressBar loads? 我必须在哪里附加此动态按钮以使它们出现在表格单元格中? - Where do I have to attach this dynamic buttons to make them appear inside the table cell? (C#)如何在按下按钮时显示新表格窗口? 我可以将其他按钮和文本框拖到其中的一个 - (C#)How do I, upon pressing a button, Make a new form window appear? One that i can drag other buttons and text boxes onto 如何通过链接使元素出现在屏幕中央? - How do I make an element appear in the center of a screen from a link? 如何在Visual Studio中正确显示MVC项目? - How do I make a MVC project appear properly in Visual Studio? 如何使控制台应用程序显示“无响应”? - How do I make a console application appear “Not Responding”? 如何在控制台应用程序中显示右键菜单? - How do I make the right click menu in a Console app appear? 如何使 iTextSharp 签名出现在文档的每一页上? - How do I make an iTextSharp signature appear on every page of a document?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM