简体   繁体   中英

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. 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'). Here is a picture: 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.

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. 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.

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.

Then in your code you replace Retry.GameObject.SetActive() with retryButton.SetActive()

And likewise for the other buttons.

Actually, it seems you are already doing this for your "countText" and "winText".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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