Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by HajiyevEl · May 05, 2020 at 10:14 AM · uitoggleupdate functionpause menutime.timescale

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by AcidmanRPGz · Aug 01, 2021 at 07:28 AM

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

289 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I save the state of a toggle button? 1 Answer

Undesirable call of Toggle's "On Value Changed" when "isOn" is changed through script 2 Answers

Toggle UI Text With UI Toggle Button 1 Answer

If I add a button to my pause menu, it doesn't show up? 0 Answers

Unity Crashs When Time.timescale = 0 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges