播放器死亡后使按钮出现-Unity3D 4.6 GUI C# [英] Making a button appear after player death - Unity3D 4.6 GUI C#

查看:239
本文介绍了播放器死亡后使按钮出现-Unity3D 4.6 GUI C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一旦玩家死亡,我将如何做到呢?

How would I make it so once the player dies a button appears?

我已经对重启级别进行了编码,并且我在屏幕上使用了该代码的按钮.我该如何做才能使按钮在播放器死之前不显示也不起作用?

I already have the restart level coded, and I have the button on screen that utilizes that code. How do I make it so the button isn't showing and isn't functional until the player is dead?

针对以下内容.

public GameObject RESTART_BUTTON;
bool isDead = false;
void Update()
{
    if (isDead == true)
    {
        RESTART_BUTTON.gameObject.SetActive(true);
        Debug.Log("Do show game object");
    }
}

void Start()
{
    if (isDead == false)
    {
        RESTART_BUTTON.gameObject.SetActive(false);
        Debug.Log("Do Not show game object");
    }
}

void OnCollisionEnter2D(Collision2D collision)
{
    Debug.Log(collision.gameObject.tag);
    if (collision.gameObject.tag == "Death")
    {
        isDead = true;
        Debug.Log("isDead_true");
    }
}

这是我的控制台输出 http://i.imgur.com/XFnOhHh.png

public void OnCollisionEnter2D(Collision2D collision)
{
    Debug.Log(collision.gameObject.tag);
    if (collision.gameObject.tag == "Death")
    {
        //Destroy(gameObject);
        isDead = true;
    } // end if including tag collision
} // End OnCollisionEnter

IEnumerator isDeath()
{
    if (isDead == true)
    {
        _animator.Play(Animator.StringToHash("Jump"));
        ;
        yield return new WaitForSeconds(2);
        Destroy(gameObject); //this will wait 5 seconds
    } // end if including boolean isDead
}

推荐答案

public GameObject YourButton;

在播放器处于活动状态时使按钮保持不活动状态.他死后,执行以下代码.

keep the button inactive while the player is alive. Once he is dead, execute the following code.

YourButton.gameObject.setActive(true);

这将激活屏幕上的按钮.

This would activate the button on the screen.

在void Update()中添加此代码,然后在void start()中将代码更改为冲突,然后更改为:

Add this code in void Update() and change the code in void start() and collision to:

 // // Update is called once per frame
 void Update () {
 if(isDead == true){
    RESTART_BUTTON.gameObject.SetActive(true);
    }
}

void Start () {
    if(isDead == false){
    RESTART_BUTTON.gameObject.SetActive(false);
    }
 }

public void OnCollisionEnter2D(Collision2D collision){
Debug.Log(collision.gameObject.tag);
if(collision.gameObject.tag == "Death"){
    isDead = true;
} 
}

那应该做有需要的.

这篇关于播放器死亡后使按钮出现-Unity3D 4.6 GUI C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆