3
\$\begingroup\$

On starting the game, a basic startmenu should load. startScreenUI is the background image, and startUI is the buttons for "play" and "exit".

I set Time.timescale to zero in Awake() so that the time would initially be set to zero.

However, the game just plays behind the start screen as normal.

My PauseMenu scripts runs perfectly, pausing the game etc.

My MoveEnemy scripts controls the movement of a wall that moves back and forth between the scene. It moves fine, on pause it stops moving. I should add that the Wall is kinematic and used gravity.

I do not understand why the initial time.Timescale is not set to zero upon starting the game and the game runs normally behind the start screen.

My code for the StartMenu:

using UnityEngine;
using System.Collections;

public class StartMenu : MonoBehaviour
{

public GameObject startUI;
public GameObject startScreenUI;
public PauseMenu otherscript; 

  private void Awake() {
    otherscript.enabled = false;
    startUI.SetActive(true);
    startScreenUI.SetActive(true);
    Time.timeScale = 0;
  }

  private void Start() {
    startUI.SetActive(true);
    startScreenUI.SetActive(true);
    Time.timeScale = 0;
  }

  public void Play() {
    startUI.SetActive(false);
    startScreenUI.SetActive(false);
    Time.timeScale = 1;
  }

  public void Quit() {
    Application.Quit();
  }
}

This is my code for the Moving of the Enemy Walls:

using UnityEngine;
using System.Collections;

public class MoveEnemy : MonoBehaviour {
  public Vector3 pos1 = new Vector3 (-7.6f, 1.1f, 7f);
  private Vector3 pos2 = new Vector3(7.6f, 1.1f, 7f);
  public float speed = 1.0f;

  // get StartMenu for bool of startUI

  public StartMenu otherScript;

  // get PlayerController for bool of "dead" to reset the static component manually
  // resets the position of the wall

  void Awake() {
    transform.position = pos1;
  }

  // moves object from pos1 to pos 2
  void Update () {
    transform.position = Vector3.Lerp(pos1, pos2, Mathf.PingPong(Time.time * speed, 1.0f));
  }
}

using the Awake() function was a troubleshooting attempt I heard worked to reset the position of the walls upon restart. But as my game does not restart properly, I do not know if this works fully yet.

and finally this is my code for the Pause Menu that actually works:

using UnityEngine;
using System.Collections;

public class PauseMenu : MonoBehaviour {

  public GameObject pauseUI;
  private bool paused = false;

  void Start() {
  }

  void Update() {
    if(Input.GetButtonDown("Pause")) {
      paused = !paused;
    }

    if(paused) {
      pauseUI.SetActive(true);
      Time.timeScale = 0;
    }

    if(!paused) {
      pauseUI.SetActive(false);
      Time.timeScale = 1;
    }
  }

  public void Resume() {
    paused = false;
  }

  public void Restart() {
  }

  public void MainMenu() {
    Application.LoadLevel(0);
  }

  public void Quit() {
    Application.Quit();
  }
}

Initially I wondered if the problem was associated with the PauseMenu's timescale function interfering with the timescale set on the startMenu, but on disabling the PauseUI completely the game still ran behind the start screen.

Can anyone think of a quick fix to this? I have been troubleshooting for a while and not really getting anywhere.

\$\endgroup\$
1
\$\begingroup\$

I believe what happens is that you set the Time.timescale to 0 in the start menu script but at the same time you have the pause menu script setting the Time.timescale to 1 because your variable pause is false. So what you could do is change your pause script so that it only is able to resume the game (set pause to false) if the game was previously paused.

Another option is to make another public Boolean variable (for example called "canRun") in the pause script then when ever the game starts (eg. Start button is pressed) you set canRun to true from the start menu script. In your pause script you adjust your if statement to check for canRun as well. Change this if statement:

 if(!paused) {
  pauseUI.SetActive(false);
  Time.timeScale = 1;
}

To look like this:

 if(!paused && canRun) {
  pauseUI.SetActive(false);
  Time.timeScale = 1;
}
\$\endgroup\$
0
\$\begingroup\$

Time.timeScale is global and is affecting all instances using time related functionality.

You are setting TimeScale to 1 every frame in your Update method since the member paused is false from the start in you Pause menu script. This is setting timescale to 1 as soon as the game is not paused, for example directly when the level is loaded, in the start screen.

if(!paused) {
  pauseUI.SetActive(false);
  Time.timeScale = 1;
}

I would recommend using methods for Pause and UnPause and restrict timescale to be controlled only in those methods.

Quick fix: make a reference to PauseMenu in StartMenu and deactivate it in Awake.

\$\endgroup\$
  • \$\begingroup\$ I added the change to the StartMenu script but it still refuses to work \$\endgroup\$ – Luke Taylor Dec 16 '15 at 3:48
  • \$\begingroup\$ public PauseMenu otherscript; private void Awake() { otherscript.enabled = false; startUI.SetActive(true); startScreenUI.SetActive(true); Time.timeScale = 0; } \$\endgroup\$ – Luke Taylor Dec 16 '15 at 3:51
  • \$\begingroup\$ Dumb question but are you sure the reference to the PauseMenu is correct. Did you forget to drag it in the inspector. Other solution might be to implement a Pause() method in the PauseMenu and set paused to true. Then call that method in the MainMenu. Let the Pause script handle changing the TimeScale. \$\endgroup\$ – Uri Popov Jun 15 '16 at 10:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.