[英]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.