Resently I built a pause screen and Time.timeScale worked perfectly. Now I tried to use the same system for an inventory and it doesn’t work when the inventory is open the time keeps on running…
As I am a beginner these both seeme tecnically the same for me. Now before I keep wasting my time on searching the problem elsewhere I’d like to know if I overlooked a error in the code.
Here’s a guide to help you figure out what is happening:
What is often happening in these cases is one of the following:
the code you think is executing is not actually executing at all
the code is executing far EARLIER or LATER than you think
the code is executing far LESS OFTEN than you think
the code is executing far MORE OFTEN than you think
the code is executing on another GameObject than you think it is
you’re getting an error or warning and you haven’t noticed it in the console window
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run? what order does it run in?
what are the values of the variables involved? Are they initialized? Are the values reasonable?
are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)
Knowing this information will help you reason about the behavior you are seeing.
You can also supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);
If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.
You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.
You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:
You must find a way to get the information you need in order to reason about what the problem is.
Sorry I had little time to explain it further then and hoped it may be a common mistake and doesn’t need futher explenation. But apperently not, so…
Now I had more time so I experimented with the debug log the results are:
Printing the time scale in the script for the inventory results in time scale 0 when the inventory is open and 1 when its closed. However when the pause menu is opend or closed it doesn’t affect the time scale.
Printing the time scale in the script for the pause menu results intime scale 0 when the pause menu is open and 1 when its closed. It too isn’t affected when the inventory is opened or closed.
Printing the time scale in other scripts shows that the time scale is only affected when the pause menu is used and not when the inventory is used.
So that means that the code is running but it only changes the time scale for this script and not for the whole project.
hope that is more usefull if not please tell me
In case interested thats the full code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class InventoryScpt : MonoBehaviour
{
bool isOpen = false;
public GameObject InventoryBg;
void Update()
{
if (isOpen)
{
Time.timeScale = 0f;
InventoryBg.SetActive(true);
}
else if(!isOpen)
{
Time.timeScale = 1f;
InventoryBg.SetActive(false);
}
if (Input.GetKeyDown(KeyCode.E))
{
isOpen = !isOpen;
}
Debug.Log(Time.timeScale + " sI");
}
}
I changed it to be even closer to the working one but it didn’t help…
You should try to avoid manipulating something as global as Time.timeScale anywhere except in a single script whose only responsibility is to control time.
Keep in mind that both of the above scripts could simulanteously be running and flipping the timeScale from 0 to 1 back and forth.
This means your keyboard E check is almost certainly being overwritten somewhere.
If you must keep this multi-point strategy, at a bare minimum, ANY script that manipulates Time.timeScale should be removed or disabled whenever it is not needed, otherwise you have mayhem.
But don’t take my word for it: find out yourself with Debug.Log(). Be sure to also print Time.frameCount so you can see which is running when in a given frame.
This is the bare minimum of information to report:
what you want
what you tried
what you expected to happen
what actually happened, log output, variable values, and especially any errors you see - links to documentation you used to cross-check your work (CRITICAL!!!)