#An Timescale doesn't work for pause

1 messages · Page 1 of 1 (latest)

simple trellis
#

You have two scripts setting the time scale independently of one another.
Whichever one runs later will determine the final value for the frame. It just seems that the Update in your PauseUI script runs after than in your QuestUI script.

You should always make sure that you only have one script setting global values. One (somewhat) easy way of doing that, is creating a list inside a singleton.
If the list has a length greater than 0, pause the game, otherwise don't.
Then any script that wants the game to be paused can add itself to the list. This way, scripts can act independently and still take influence on global values - without interfering with each other.

dawn crystal
simple trellis
#

Time.timeScale is a static value. Anything else would make no sense frankly, because a game can't run at multiple speeds at the same time. But that also means, that you have to be careful where you set it from.

Generally speaking, you would have some kind of GameManager that is a singleton and takes care of setting values like this, instead of individual scripts doing it.
If you don't know what a singleton is, look it up; it's very useful to know (though it should not be overused either).

Assuming you have such a GameManager, the implementation I mentioned above would look somewhat as follows.
First, you will need a variable in your manager to store all objects that want the game to be paused:

private readonly HashSet<MonoBehaviour> pauseRequestors = new HashSet<MonoBehaviour>();```
I am using a `HashSet` here, because those cannot contain the same object multiple times (which will managing the whole thing easier).

Second, you'll want to create methods in your manager to pause and unpause the game (or, more precisely, add and remove pause requestor instances): ```cs
public void Pause(MonoBehaviour req)
{
  // If the object requesting the pause is not null
  if(req != null)
  {
    // Add it to the set
    pauseRequestors.Add(req)
    // Update the time scale
    UpdatePause();
  }
}

public void Unpause(MonoBehaviour req)
{
  // If the object requesting to unpause is not null
  if(req != null)
  {
    // Remove it from the set
    pauseRequestors.Remove(req)
    // Update the time scale
    UpdatePause();
  }
}

public void ForceUnpause()
{
  // Remove all pause-requesting objects from the set
  pauseRequestors.Clear();
  // Update the time scale
  UpdatePause();
}

private void UpdatePause ()
{
  // Set time scale to 0 if the set contains any entries, otherwise set it to 1
  Time.timeScale = pauseRequestors.Count > 0 ? 0f : 1f;
}```
#

Given this, you can re-write your UI scripts to pause/unpause using the manager, rather than by setting the time scale themselves:
GameManager.Instance.Pause(this); instead of Time.timeScale = 0f;
and
GameManager.Instance.Unpause(this); instead of Time.timeScale = 1f;

#

Of course, this system could be expanded upon arbitrarily. For example, you could implement an interface to notify all pausing objects if unpausing is forced.

dawn crystal
#

is that req was kind of class or something?

simple trellis
#

req is not a class. It's a method parameter in both cases...

dawn crystal
#

using StarterAssets;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.SceneManagement;

public class PauseSetting : MonoBehaviour
{
private readonly HashSet<MonoBehaviour> pauseRequestors = new HashSet<MonoBehaviour>();
private StarterAssetsInputs starterAssets;

public static bool pauseGame = false;
public GameObject ikan;
public GameObject sensor;

private int stop = 1; 

// Start is called before the first frame update
void Start()
{
    starterAssets = GetComponent<StarterAssetsInputs>();

}

private void Update()
{
    if (starterAssets.menu)
    {
        stop++;
        starterAssets.menu = false;
    }

    if (stop > 2) stop = 1;

    if (stop == 1) Unpause();

    if (stop == 2) Paused();
}

public void Pause(MonoBehaviour req)
{
    // If the object requesting the pause is not null
    if (req != null)
    {
        // Add it to the set
        pauseRequestors.Add(req);
      // Update the time scale
        UpdatePause();
    }
}

public void Unpause(MonoBehaviour req)
{
    // If the object requesting to unpause is not null
    if (req != null)
    {
        // Remove it from the set
        pauseRequestors.Remove(req);
      // Update the time scale
        UpdatePause();
    }
}

public void ForceUnpause()
{
    // Remove all pause-requesting objects from the set
    pauseRequestors.Clear();
    // Update the time scale
    UpdatePause();
}

private void UpdatePause()
{
    // Set time scale to 0 if the set contains any entries, otherwise set it to 1
    Time.timeScale = pauseRequestors.Count > 0 ? 0f : 1f;
}

}
is this correct? but there's an error in line 36

simple trellis
#

No, this is not correct.
I'm pretty sure you implemented it in the wrong script.
You also didn't implement it in a singleton, which makes the whole thing pointless.
And you also are not calling the pause/unpause methods as they are supposed to.

Did you read the comments I left in the code, and the explanation I gave on it?

dawn crystal
simple trellis
#

I explicitly did not include any classes in my code.
As I stated before, this code was meant to be placed inside some kind of GameManager class. And that class would have to be implemented as a singleton.

dawn crystal
simple trellis
#

Does that means i have to use your code in GameManager script?
Yes. I've said so two times now.

And how to implement as singleton?
I also have alluded to that previously.
Are you sure you read what I wrote earlier in its entirety?

dawn crystal
#

Yes i do, but maybe i little bit confused

dawn crystal
simple trellis
#

Don't guess, please. Programming is not a guessing game.
And no, it is not. If it were, I wouldn't have needed to specify that it needs to be added to a singleton class.

dawn crystal
#

Okay my bad, i Will read And understand it thoroughly until i get it

simple trellis
#

And if you do not know what a singleton is - as I said previously - watch a tutorial. There are plenty out there, some more advanced than others. But as long as you get the core principle, you should be good.