Reiss Hamilton
Reiss Hamilton

Reputation: 1

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

Upvotes: 0

Views: 487

Answers (3)

Giawa
Giawa

Reputation: 1371

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

Upvotes: 0

user18817353
user18817353

Reputation: 1

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.

Upvotes: 0

user16716191
user16716191

Reputation:

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.

Upvotes: 0

Related Questions