简体   繁体   English

如何从设置值禁用然后重新启用游戏 object?

[英]How do I disable then re-enable a game object from set values?

I am creating an asteroid copy for a college assignment and I've completed most of it except for the UI.我正在为大学作业创建一个小行星副本,除了 UI 之外,我已经完成了大部分副本。 The only problem is the lives, I've set it so the setActive on the game objects are false then true.唯一的问题是生命,我已经将它设置为游戏对象上的 setActive 先假后真。 Once the players lives reach 2 the object is disabled but once the player loses all three lives and the lives are put back to three the object will not re-enable.一旦玩家的生命达到 2,object 将被禁用,但一旦玩家失去所有三个生命并且生命回到三个,object 将不会重新启用。 The code should be attached here any help would be appreciated.代码应附在此处,如有任何帮助,我们将不胜感激。

在此处输入图像描述

Add a boolean and set it to true when all three lives are lost and then when enabling the gameObject check if said boolean is true and if it is, don't enable it.添加一个 boolean 并在所有三个生命都丢失时将其设置为真,然后在启用游戏对象时检查所说的 boolean 是否为真,如果是,则不要启用它。

I would try creating a bool that is set true when the lives are at zero.我会尝试创建一个 bool,当生命为零时设置为真。 Then make the gameObject not enabled if the bool is true.如果 bool 为真,则不启用游戏对象。

Update will only be called in LivesUI when that LivesUI is active.仅当LivesUI处于活动状态时,才会在LivesUI中调用Update If you've disabled the LivesUI gameobject then Update will never run again (unless the gameObject is set active again by something else), causing the gameObject to never get re-enabled.如果您禁用了LivesUI游戏对象,则Update将永远不会再次运行(除非游戏对象被其他东西再次设置为活动状态),导致游戏对象永远不会重新启用。 Instead of having the code run in Update , I would suggest having whatever code changes the values of manager.lives to also fire a message to your UI to update which UI elements should be shown.与其让代码在Update中运行,我建议让任何代码更改manager.lives的值,同时向您的 UI 发送消息以更新应显示的 UI 元素。

For example, this (untested) code may be helpful:例如,这段(未经测试的)代码可能会有所帮助:

public void SetLives(int lives)
{
    manager.lives = lives;

    var livesTransform = transform.Find("UI/Lives");
    for (int i = 0; i < livesTransform.childCount; i++)
    {
        livesUi.getChild(i).gameObject.SetActive(lives > i);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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