- Home /
Duplicate Question
I Need Help converting somthing to C#
i am rewriting my pause menu script in C# and i am having a bit of a problem with freezing the game. it sounds very simple at first, but my game does not contain a constant time scale, the time scale changes. this worked perfectly in js., but it just won't do the same in C#
void OnGUI(){
if (GUI.Button(new Rect(Screen.width/2,Screen.height/2,50,30),"Button")){
Active = !Active;
}
}
void Update(){
_previousTimeScale = (Time.timeScale != 0) ? (Time.timeScale) : (_previousTimeScale);
if (Active == true){
Time.timeScale = 0;
}else{
Time.timeScale = _previousTimeScale;
}
}
What's the problem, exactly? Is there an error message? Is something happening that's not supposed to? Could you give us more information to work with?
don't use update. search for 1000,000,000 questions on this
Answer by Kiwasi · May 23, 2014 at 03:03 AM
Remove the whole thing from update and add a pause method. Removing from update will improve efficiency
public static bool isActive;
private static float previousTimeScale
void OnGUI () {
if (GUI.Button(new Rect(Screen.width/2,Screen.height/2,50,30),"Button")){
Pause();
}
}
public static void Pause () {
isActive = !isActive;
if(!isActive){
previousTimeScale = Time.TimeScale;
Time.TimeScale = 0;
} else {
Time.TimeScale = previousTimeScale;
}
}
Making this all static will allow you to pause your game from anywhere simply by calling the Pause() method. Making isActive static will let you check if the game is paused from anywhere. I would suggest changing the name to isPaused for clarity.
never $$anonymous$$d, i got it now...
if(GUI.Button(new Rect(Screen.width - 100, 10, 90, 90), PausePic)){
Active = !Active;
}
_previousTimeScale = (Time.timeScale != 0) ? (Time.timeScale) : (_previousTimeScale);
if (Active == true){
Time.timeScale = 0;
}else{
Time.timeScale = _previousTimeScale;
}
Answer by PippyLongbeard · May 23, 2014 at 02:03 AM
Personally, I would do this by assigning the '_previousTimeScale' variable to the 'Time.timeScale' property at the beginning, like so:
int _previousTimeScale;
void Start
{
_previousTimeScale = Time.timeScale;
}
This will make a solid base time scale at the beginning to reference from. There's no need to use a conditional operator that keeps reassigning the same value over and over. I haven't tested this, but that should fix it. Hope this helps!
Answer by Tarlius · May 23, 2014 at 05:06 AM
If you are getting different behaviour after converting to c#, it would be a good idea to provide the original js as well as saying what the problem/difference actually is.
Is this your whole source file? My personal experience with java is somewhat limited, but I seem to remember that java doesn't require you to actually declare the class (or anything really... Use #pragma strict always!). Did you wrap the code in:
using UnityEngine;
public class PauseClass : MonoBehaviour { // Note the class name must match the .cs filename
// your code here
}
Also, BoredMormon's suggestion about refactoring to not use update is a good one. If you really need to track timescale that much, it might be a good idea to write a wrapper for updating time (although if it is every frame then your approach is ok).
actually, the script is over 1000 lines because it is a pause menu with pages within pages. the whole thing is weaved to a public class called resources where i serialize and save the players armor, money, and progress. the problem i am having is that the pause thing does not want to work. my player encounters orbs called atoms that alter the players size or time scale. thats why i needed one that alters time.
Follow this Question
Related Questions
How to to do a progress bar wich scale down in 6 seconds ? (timeScale used) 0 Answers
Can't really pause my game 1 Answer
Unity how to find load time? 1 Answer
Unity Time.timeScale is out of range. Any chance to set it to 360? 1 Answer
2nd independent deltaTime and timeScale variables, or a way to mimic this? 1 Answer