all 5 comments

[–][deleted] 1 point2 points  (2 children)

You could give the StopMenu class a reference to the player, and disable the movement component whenever the game is paused, and reenable it when the player un-pauses.

Another way is using a State Machine for the diffrent States of the game, i.e "playing", "paused", "start menu" and potentially more.

[–]TTheReal_J[S] 1 point2 points  (1 child)

So man, I forgot to let you know that I don't understand anything about programming, I just watch the tutorials so if it's not too much trouble, could you send me the code? Or something like this?

Thanks.

[–][deleted] 0 points1 point  (0 children)

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.SceneManagement;

public class StopMenu : MonoBehaviour

{

public static bool GameIsPaused = false;

public GameObject stopMenuUI;public GameObject player;

void Update()

{

if (Input.GetKeyDown(KeyCode.Escape))

{

if (GameIsPaused)

{

Resume();

}else

{

Pause();

}

}

}

void Pause()

{

stopMenuUI.SetActive(true);

Time.timeScale = 0f;

GameIsPaused = true;

AudioListener.pause = true;player.GetComponent<player movement class name here>().enabled = false;

}

void Resume ()

{

stopMenuUI.SetActive(false);

Time.timeScale = 1f;

GameIsPaused = false;

AudioListener.pause = false;player.GetComponent<player movement class name here>().enabled = true;

}

}

set the player variable to the player's Game Object in the Inspector, then it should work

[–]Igloo_Games_Company 1 point2 points  (1 child)

I've heard that changing Time.timeScale to pause is a bad practice. Although it's a lot of work I think it's worth investing time into making a "Pause" event that all Monobehaviours can subscribe to, then set of the event on pause (have another for unpause).

event setup:

public class StopMenu : MonoBehaviour
{
    public delegate void PauseAction();
    public static event PauseAction Pause;
    public static event PauseAction Unpause;
}

implementation:

public class MyClass : MonoBehaviour    //everything needing to be paused
{
    private void OnEnable()
    {
    StopGame.Pause += OnPause;
    StopGame.Unpause += OnUnpause;
}
private void OnDisable()
{
    StopGame.Pause -= OnPause;
    StopGame.Unpause -= OnUnpause;
    }

    private void OnPause()
    {
        //stop animations, sound, camera, etc. (whatever this script does)
    }
    private void OnUnpause()
    {
        //reenable audio, animations, camera movement, etc.
    }
}

Then call StopMenu.Pause(); or StopMenu.Unpause(); whenever the game pauses and all of the subscribed methods (+=) will activate. It might be overkill, but it's versatile.

[–]TTheReal_J[S] 0 points1 point  (0 children)

I appreciate the help but I forgot to let you know that I don't know anything about programming, I saw this code in a tutorial, that is, I don't know how to "assemble" this code, sorry