Using Toggle when Time.timeScale = 0 (Pause Menu)

Hello all! Like i said above i can’t use toggle in my pause menu. Pressing on button gives me debug.log from first update function but never goes further. I found somewhere that i need to create timer myself and switch Time.deltaTime to my timer deltaTime. I tried it and… i wasn’t successful.
What i’m using:
Toggle (download link in video description)

My remaded ToggleController script below( i added new variable to get TimeScaleIndependentUpdate.deltaTime (script is next below Toggle script) and swapped Time.deltaTime (BTW, i used “var timer = new TimeScaleIndependentUpdate();” forst, but got error “You are trying to create a MonoBehaviour using the ‘new’ keyword. This is not allowed.” so i just googled a little and used what i have now (source of resolve here) :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ToggleControllerWithTimer : MonoBehaviour
{
    public bool isOn;

    public Color onColorBg;
    public Color offColorBg;

    public Image toggleBgImage;
    public RectTransform toggle;

    public GameObject handle;
    private RectTransform handleTransform;
    public Sprite onHandle;
    public Sprite offHandle;

    private float handleSize;
    private float onPosX;
    private float offPosX;

    public float handleOffset;

    public GameObject onIcon;
    public GameObject offIcon;

    private AudioManager audioManager;

    public float speed;
    static float t = 0.0f;

    private bool switching = false;


    void Awake()
    {
        handleTransform = handle.GetComponent<RectTransform>();
        RectTransform handleRect = handle.GetComponent<RectTransform>();
        handleSize = handleRect.sizeDelta.x;
        float toggleSizeX = toggle.sizeDelta.x;
        onPosX = (toggleSizeX / 2) - (handleSize / 2) - handleOffset;
        offPosX = onPosX * -1;
    }

    void Start()
    {
        audioManager = FindObjectOfType<AudioManager>();

        if (isOn)
        {
            toggleBgImage.color = onColorBg;
            handleTransform.localPosition = new Vector3(onPosX, 0f, 0f);
            onIcon.gameObject.SetActive(true);
            offIcon.gameObject.SetActive(false);
        }
        else
        {
            toggleBgImage.color = offColorBg;
            handleTransform.localPosition = new Vector3(offPosX, 0f, 0f);
            onIcon.gameObject.SetActive(false);
            offIcon.gameObject.SetActive(true);
        }
    }

    private void OnEnable()
    {
        if (PlayerPrefs.GetInt("Muted", 0) == 0)
        {
            handle.gameObject.GetComponent<Image>().sprite = onHandle;
            isOn = true;
        }
        else
        {
            handle.gameObject.GetComponent<Image>().sprite = offHandle;
            isOn = false;
        }
    }

    void Update()
    {
        if (switching)
        {
            Toggle(isOn);
        }
        Debug.Log(isOn);
    }

    public void PauseMusic()
    {
        audioManager.ToogleSound();
        UpdateVolume();
    }

    void UpdateVolume()
    {
        if (PlayerPrefs.GetInt("Muted", 0) == 0)
        {
            AudioListener.volume = 1;
        }
        else
        {
            AudioListener.volume = 0;
        }
    }

    public void DoYourStaff()
    {
        if(isOn == false)
        {
            PauseMusic();
            handle.gameObject.GetComponent<Image>().sprite = offHandle;
        }
        else
        {
            PauseMusic();
            handle.gameObject.GetComponent<Image>().sprite = onHandle;
        }
        Debug.Log(isOn);
    }

    public void Switching()
    {
        switching = true;
        Debug.Log(isOn);
    }

    public void Toggle(bool toggleStatus)
    {
        if (onIcon.activeSelf == false || offIcon.activeSelf == false)
        {
            onIcon.SetActive(true);
            offIcon.SetActive(true);
        }

        if (toggleStatus)
        {
            toggleBgImage.color = SmoothColor(onColorBg, offColorBg);
            Transparency(onIcon, 1f, 0f);
            Transparency(offIcon, 0f, 1f);
            handleTransform.localPosition = SmoothMove(handle, onPosX, offPosX);
        }
        else
        {
            toggleBgImage.color = SmoothColor(offColorBg, onColorBg);
            Transparency(onIcon, 0f, 1f);
            Transparency(offIcon, 1f, 0f);
            handleTransform.localPosition = SmoothMove(handle, offPosX, onPosX);
        }
    }

    Vector3 SmoothMove(GameObject toggleHandle, float startPosX, float endPosX)
    {
        GameObject clock = new GameObject();
        clock.AddComponent<TimeScaleIndependentUpdate>();
        var timer = clock.GetComponent<TimeScaleIndependentUpdate>();
        //var timer = new TimeScaleIndependentUpdate();
        Vector3 position = new Vector3(Mathf.Lerp(startPosX, endPosX, t += speed * timer.deltaTime), 0f, 0f);
        StopSwitching();
        return position;
    }

    Color SmoothColor(Color startCol, Color endCol)
    {
        GameObject clock = new GameObject();
        clock.AddComponent<TimeScaleIndependentUpdate>();
        var timer = clock.GetComponent<TimeScaleIndependentUpdate>();
        //var timer = new TimeScaleIndependentUpdate();
        Color resultCol;
        resultCol = Color.Lerp(startCol, endCol, t += speed * timer.deltaTime);
        return resultCol;
    }

    CanvasGroup Transparency(GameObject alphaObj, float startAlpha, float endAlpha)
    {
        GameObject clock = new GameObject();
        clock.AddComponent<TimeScaleIndependentUpdate>();
        var timer = clock.GetComponent<TimeScaleIndependentUpdate>();
        //var timer = new TimeScaleIndependentUpdate();
        CanvasGroup alphaVal;
        alphaVal = alphaObj.gameObject.GetComponent<CanvasGroup>();
        alphaVal.alpha = Mathf.Lerp(startAlpha, endAlpha, t += speed * timer.deltaTime);
        return alphaVal;
    }

    void StopSwitching()
    {
        if (t > 1.0f)
        {
            switching = false;

            t = 0.0f;
            switch (isOn)
            {
                case true:
                    isOn = false;
                    DoYourStaff();
                    break;

                case false:
                    isOn = true;
                    DoYourStaff();
                    break;
            }

        }
    }

}

Timer script from here. Same script remaded by me below (actually i didn’t remade anything, just changed “GameStateManager.SharedInstance.Paused()” to “Time.timeScale == 0”) :

using UnityEngine;
using System.Collections;

public class TimeScaleIndependentUpdate : MonoBehaviour
{
    

    //inspector fields
    public bool pauseWhenGameIsPaused = true;

    //private fields
    float previousTimeSinceStartup;

    protected virtual void Awake()
    {
        previousTimeSinceStartup = Time.realtimeSinceStartup;
    }

    protected virtual void Update()
    {
        float realtimeSinceStartup = Time.realtimeSinceStartup;
        deltaTime = realtimeSinceStartup - previousTimeSinceStartup;
        previousTimeSinceStartup = realtimeSinceStartup;

        //It is possible (especially if this script is attached to an object that is 
        //created when the scene is loaded) that the calculated delta time is 
        //less than zero.  In that case, discard this update.
        if (deltaTime < 0)
        {
            Debug.LogWarning("Delta time less than zero, discarding (delta time was "
                + deltaTime + ")");

            deltaTime = 0;
        }

        //NOTE: You will want to change "GameStateManager.SharedInstance.Paused()" 
        //to whatever you use to check if the game has been paused by the user
        if (pauseWhenGameIsPaused && Time.timeScale == 0 ) 
        {
            deltaTime = 0;
        }
    }

    public IEnumerator TimeScaleIndependentWaitForSeconds(float seconds)
    {
        float elapsedTime = 0;
        while (elapsedTime < seconds)
        {
            yield return null;
            elapsedTime += deltaTime;
        }
    }

    #region Property methods

    public float deltaTime { get; private set; }

    #endregion
}

And my pause script below:

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

public class PauseMenu : MonoBehaviour
{
    private AudioManager audioManager;

    public GameObject onSprite;
    public GameObject offSprite;

    public GameObject pauseMenuUI;
    public Animator transitionAnim;

    public GameObject BG_Panel;


    private void Start()
    {
        audioManager = FindObjectOfType<AudioManager>();
        UpdateIcon();
    }

    public void PauseMusic()
    {
        audioManager.ToogleSound();
        UpdateIcon();
    }

    void UpdateIcon()
    {
        if (PlayerPrefs.GetInt("Muted", 0) == 0)
        {
            AudioListener.volume = 1;
            onSprite.SetActive(true);
            offSprite.SetActive(false);
        }
        else
        {
            AudioListener.volume = 0;
            onSprite.SetActive(false);
            offSprite.SetActive(true);
        }
    }

    public void Resume()
    {
        if (pauseMenuUI.activeSelf == true && BG_Panel.activeSelf == false)
        {
            pauseMenuUI.SetActive(false);
            Time.timeScale = 1f;
        }
        else
        {
            if (pauseMenuUI.activeSelf == true && BG_Panel.activeSelf == true)
            {
                pauseMenuUI.SetActive(false);
                Time.timeScale = 0f;
            }
        }
    }

    public void Pause()
    {
        if (pauseMenuUI.activeSelf == false)
        {
            pauseMenuUI.SetActive(true);
            Time.timeScale = 0f;
        }
    }

    private void OnApplicationPause()
    {
        //pauseMenuUI.SetActive(true);
        //Time.timeScale = 0f;
    }

    public void LoadMenu()
    {
        StartCoroutine(ChangeScene());
    }

    public void QuitGame()
    {
        Application.Quit();
        Debug.Log("QUIT!");
    }

    IEnumerator ChangeScene()
    {
        transitionAnim.SetTrigger("endChangeScene");
        yield return new WaitForSeconds(0.0f);
        SceneManager.LoadScene("Menu"); //SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1); - if used will load next scene by index
        Time.timeScale = 1f;
    }
}

I’m newbie, so please help?

I suspect that since this is over a year old, you’ve solved it, but I am having the same issue, if you have a solution, what is it? @HajiyevEl