0
\$\begingroup\$

I have a pause menu for my game which instantiates a texture and sets a text frame to active and slides both objects onto the screen using the code below:

Instantiating the menu texture and activating the text frame:

public void MenuPressed ()
{
    Vector3 newPosition = new Vector3 (-13, -0.06f, -2);
    Quaternion newRotation = Quaternion.Euler (0, 0, 0);
    Instantiate (menu, newPosition, newRotation);

    menuButton.SetActive (false);

    textFrame.SetActive (true);

    Time.timeScale = 0;
}

Moving the menu items:

float xLimit = -5;
float speed = 0.6f;

void Start ()
{
    StartCoroutine ("MoveMenu");
}

IEnumerator MoveMenu ()
{
    while (true)
    {
        transform.position += Vector3.right * speed;
        if (gameObject.name == "Main Text Frame")
            xLimit = -7;
        if (transform.position.x > xLimit)
            yield break;
        yield return new WaitForEndOfFrame ();
    }
}

Now this all works fine, but the issue that I have is on a multiple pause situation. When the pause button is pressed and spawns the menu and activates the text, the game pauses as expected. One of the options on the menu is to resume playing, when the pressed the game carries on as expected.

The problem I am having is that if the pause button is pressed again, the menu texture spawns in and slides on screen as intended, but the frame for the text does not. The frame gets set active but does not actually move.

Any ideas for how to fix this?

P.S

Here is the code I use to resume the game after it had been paused

public void Button1Pressed ()
{
    Time.timeScale = 1;
    GameObject menu = GameObject.FindWithTag ("Menu");
    Destroy (menu);
    gameController.paused = false;
    textFrame.SetActive (false);
    menuButton.SetActive (true);
    textFrame.transform.position = new Vector3 (-15.2f, -4.0f, -2.5f);
}
\$\endgroup\$
  • \$\begingroup\$ You have to show some more code about textFrame, what else you are doing with it? \$\endgroup\$ – Hamza Hasan Mar 16 '16 at 15:33
  • \$\begingroup\$ At the moment I am currently doing nothing with it, this is the only time I am using it \$\endgroup\$ – Weeksy Mar 16 '16 at 15:57
0
\$\begingroup\$

When you yield break from MoveMenu, it ends that coroutine forever. So when the text reaches its destination for the first time, that coroutine stops until you start it again. When you reactivate the text, you're neglecting to restart the coroutine (Start is not called on reactivation.) So you should just have something like this in the component containing MoveMenu:

public class MenuMover : MonoBehaviour
{
    void Start()
    {
        StartMove();
    }

    public void StartMove()
    {
        StartCoroutine("MoveMenu");
    }

    IEnumerator MoveMenu()
    {
        // do your thing
    }
}

Then you should manually call StartMove whenever you reactivate the text.

For convenience you might also like to reset the position of the object in StartMove:

public Vector3 offscreen_start_pos;

public void StartMove()
{
    transform.position = offscreen_start_pos;
    StartCoroutine("MoveMenu");
}
\$\endgroup\$
  • \$\begingroup\$ Thank you for your answer, this works for me in terms of having the text correctly appear on each pause. However, on the first pause only, the text does not align in the correct position when it stops, but it does on every pause after that. Do you have any idea why this is? \$\endgroup\$ – Weeksy Mar 17 '16 at 10:51
  • \$\begingroup\$ It's hard to say, but maybe you've got two MoveMenus running at once on the same object? Try removing the StartMove call from Start \$\endgroup\$ – Nox Mar 17 '16 at 12:43
  • \$\begingroup\$ I managed to find a work around where I check in Start whether the Game Object is the Text Frame or the texture and this solved my problem \$\endgroup\$ – Weeksy Mar 17 '16 at 13:06

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.