【问题标题】:How do I make buttons appear at the end of the game?如何让按钮出现在游戏结束时?
【发布时间】:2016-04-03 16:25:46
【问题描述】:

前几天我开始使用Unity,使用官方教程学习。我已经完成了第一场比赛,并且开始喜欢编程。我对此很陌生,并且认为如果我将这些教程游戏做得更好,它将对我有所帮助。一切都很顺利,但现在我被困住了。我想要一个在我赢得比赛时出现的菜单。我制作了菜单,它有 3 个按钮(“再试一次”、“菜单”和“退出”)。这是一张图片:http://prntscr.com/9jy71h。这些按钮可以工作,但我唯一的问题是它们在整个游戏中都出现了很长时间,而我只想要它们在最后。我尝试了很多,但我似乎一直得到这个错误:'Retry' doesn't exist in the current context.

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);
       }
   }
}

为什么他不能识别我制作的游戏对象? (您在图片上看到的按钮)抱歉我的英语不好,感谢您抽出宝贵时间阅读此内容并尽可能回答。

编辑:http://prntscr.com/9jybke 这些都是错误。但是,如果我删除顶部和底部的 6 条 gameobjective(bool) 行,它工作正常,但我一直打开菜单。

【问题讨论】:

标签: c# user-interface unity3d


【解决方案1】:

您缺少的是脚本对按钮游戏对象的引用,以便您可以打开/关闭它们。

如果你添加到你的脚本中

public GameObject retryButton;

对于其他按钮也是如此,然后在 Unity 中的检查器中为您的脚本将三个按钮游戏对象拖动到检查器中它们对应的“插槽”。

然后在您的代码中将Retry.GameObject.SetActive() 替换为retryButton.SetActive()

对于其他按钮也是如此。

实际上,您似乎已经为您的“countText”和“winText”这样做了。

【讨论】:

  • 我是在 Menu.cs 脚本中完成的。所以基本上我需要在这个脚本中完成所有这些并删除 Menu.cs 脚本?但是,如果我想在我的 Menu.cs 中进行此操作,那么我不能说何时启用按钮,因为 Menu.cs 不知道何时有人赢得了比赛。或者如果我对他说,他知道吗?那怎么办?
  • 您对代表有任何想法吗?为此目的使用委托,因为它们被用于相同目的,即广播带有信息的事件,然后任何类都可以收听它。如果你愿意,请告诉我
【解决方案2】:

尝试将按钮放在游戏对象中并使用对象.SetActive(false) 它对我有用。

【讨论】:

    猜你喜欢
    • 2019-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多