0

So, i have buttons for my game that include : Main Menu button , Restart button and Play button. Everything works fine, but occasionally one of the button's text doesn't load , one of them doesn't work. I'm not sure how to fix this, because it happens randomly and not every time.

The code for the buttons :

GameObject[] pauseObjects;
void Start()
{
    Time.timeScale = 1;
    pauseObjects = GameObject.FindGameObjectsWithTag("ShowOnPause");
    hidePaused();
}


public void showPaused()
{
    foreach (GameObject g in pauseObjects)
    {
        g.SetActive(true);
    }
}


public void hidePaused()
{
    foreach (GameObject g in pauseObjects)
    {
        g.SetActive(false);
    }
}

public void LoadLevel(string level)
{
    Application.LoadLevel(level);
}

public void pauseControl()
{
    if (Time.timeScale == 1)
    {
        Time.timeScale = 0;
        showPaused();
    }
    else if (Time.timeScale == 0)
    {
        Time.timeScale = 1;
        hidePaused();
    }
}
1
  • I'm sorry I need a little bit of clarification. Randomly, one of your three buttons stops working, and has no text? If so, can you show us the code that sets the buttons text and activates/deactivates the button? The above code doesn't really help us, unless your buttons are all in "pauseObjects"... Commented May 16, 2016 at 19:15

1 Answer 1

0

The way your UI is setup is not right. If you have to loop through everything during pause, you are doing it wrong. I have answered a similar question before and will just copy and paste this here.

Use different Canvas to separate your UI and group them depending on when they are displayed. Then you can toggle the whole Canvas on/off. Your [post][1] before this post shows that you are using image as button and you have a different script attached to two images you are using as buttons. Use Buttons for buttons and image for non-clickable objects. Simple as that. So change those images to buttons.

You don't need the GameOver scene. A GameOverCanvas is fine. The GameObjects in bold are the parent Objects(Canvas). The ones under that starts with '-' are the child GameObjects.

Step 1:

Create Canvas and name it MainMenuCanvas (First UI to display when game Loads).Create each child button and rename them as below(GameObject->UI->Button):

-playButton;
-settingsButton;
-exitGameButton;

Attach the MainMenuCanvas script to the MainMenuCanvas Object.

Step 2:

Create a Canvas and name it GameCanvas (Displayed during game).Create each child button and rename them as below(GameObject->UI->Button):

-pauseButton
-jumpButton

Attach the GameCanvas script to the GameCanvas Object.

Step 3:

Create a Canvas and name it PauseCanvas (Displayed when pause button is clicked).Create each child button and rename them as below(GameObject->UI->Button):

-resumeButton;
-backToMainMenuButton;
-settingsButton;
-exitGameButton;

Attach the PauseCanvas script to the PauseCanvas Object.

Step 4:

Create a Canvas and name it SettingsCanvas (Displayed when settings button is clicked).Create each child button and rename them as below(GameObject->UI->Button):

-backButton;

Attach the SettingsCanvas script to the SettingsCanvas Object.

Step 5:

Create a Canvas and name it GameOverCanvas (Displayed when Game is Over or player is killed).Create each child button and rename them as below(GameObject->UI->Button):

-playAgainButton;
-backToMainMenuButton;
-exitGameButton;

Attach the GameOverCanvas script to the GameOverCanvas Object.

Step 6:

In the Game scene, make sure that only GameCanvas is enabled. The rest of the canvas should be manually disabled.

Step 7:

In the Menu scene, make sure that only MainMenuCanvas is enabled. The rest of the canvas should be manually disabled.

Once you get this setup correctly, the UI code templates I provided should work. No more UI overlapping or text disappearing. You can easily add or remove features.

Your UI setup should look like the image below.

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Everything works perfectly fine ! Thanks for putting in this much effort and helping me out. You're a legend.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.