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; 
		}
}

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.

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).

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!