[英]How do I make buttons appear at the end of the game?
幾天前,我啟動了Unity,並使用了官方教程進行學習。 我已經有了第一款游戲,而且我開始喜歡編程。 我對此很陌生,如果我將這些教程游戲做得更好,它將對我有幫助。 一切都很好,但現在我被困住了。 我想要在我贏得比賽時出現的菜單。 我做了菜單,它有3個按鈕(“重試”,“菜單”和“退出”)。 這是一張圖片: http : //prntscr.com/9jy71h 。 這些按鈕有效,但我唯一的問題是它們在整個游戲中都出現了很長的時間,而我只希望它們在最后。 我嘗試了很多,但似乎一直都遇到此錯誤: “ 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);
}
}
}
他為什么不能識別我制作的我的游戲對象? (您在圖片上看到的按鈕)對不起,我的英語不好,謝謝您抽出寶貴的時間閱讀本文,並在可能的情況下回答。
編輯: http : //prntscr.com/9jybke這些都是錯誤。 但是,如果我刪除頂部和底部的6個gameobjective(bool)行,效果很好,但是我一直都在打開菜單。
您缺少的是腳本中對按鈕游戲對象的引用,因此您可以打開/關閉它們。
如果您添加到腳本
public GameObject retryButton;
同樣,對於其他按鈕,然后在檢查器中的Unity中,將腳本中的三個按鈕GameObject拖到檢查器中它們相應的“插槽”中。
然后在您的代碼中,用Retry.GameObject.SetActive()
替換retryButton.SetActive()
其他按鈕也一樣。
實際上,看來您已經為“ countText”和“ winText”執行了此操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.