Home > Enterprise >  How do I disable then re-enable a game object from set values?
How do I disable then re-enable a game object from set values?

Time:04-16

I am creating an asteroid copy for a college assignment and I've completed most of it except for the UI. The only problem is the lives, I've set it so the setActive on the game objects are false then true. 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. The code should be attached here any help would be appreciated.

enter image description here

CodePudding user response:

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.

CodePudding user response:

I would try creating a bool that is set true when the lives are at zero. Then make the gameObject not enabled if the bool is true.

CodePudding user response:

Update will only be called in LivesUI when that LivesUI is active. 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. 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.

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);
    }
}
  • Related