how to Pause game if score exceeds a number and display text and button to resume.
Hi Gurus I want to create a intractable button and then add on click action to it. Its not working now. Is there any example for this pls. My objective is to pause the game once user reaches a score. Enable a button along with some text. Once player clicks this button i want to resume by adding LevelResume function under onclick. The problem currently is the button is not clickable. Currently i am attaching the Resume button to Level_manager script on levelmanager object.
Can you please help.
public class Level_manager : MonoBehaviour { public GameObject resume;
void Update()
{
if (ScoreManager.SavedScore == 10)
{
resume.SetActive(true);
Time.timeScale = 0;
}
}
public void LevelResume()
{
Time.timeScale = 1;
resume.SetActive(false);
}
}
Answer by tkamruzzaman · Dec 05, 2016 at 05:17 PM
Your Update() checks score == 10 and sets timeScale to 0 even if you set it back to 1 it sets it to 0. You can simply prevent this by adding a bool to the if() statement.
public class Level_manager : MonoBehaviour
{
public GameObject resume;
private bool isResumeShown;
void Update()
{
if (ScoreManager.SavedScore == 10 && !isResumeShown)
{
isResumeShown= true;
resume.SetActive(true);
Time.timeScale = 0;
}
}
public void LevelResume()
{
Time.timeScale = 1;
resume.SetActive(false);
}
}
I have a similar script that is not working... When the game is paused buttons don't work. I think that this happens because of timescale that is 0. Anyways you are right that you must check if the game is paused or not.
When Time.timeScale is zero button's OnClick event does work. You just have to assign timeScale to one upon button (resume button) click.
Thanks although i endedup deactivating and activating required game objects ins$$anonymous$$d of pausing the game, your approach did help me, i used the additional bool to check for activate/deactivate objects.
Trying to finish the game too quickly , stumbling on logical problems :)
Answer by karmik · Dec 07, 2016 at 04:12 AM
My problem is same as eRlind, i went ahead with deactivating and activating required objects instead of pausing. Its looks much cleaner as the enemies will stop in their dying anim when the game is paused. Deactivating the game objects made more sense to me