Search Unity

How to disable timer from Main Menu Settings?

Discussion in 'Game Design' started by Idylikos, Jul 14, 2022.

  1. Idylikos

    Idylikos

    Joined:
    Jun 13, 2022
    Posts:
    2
    Hello,
    I'm a total beginner on Unity and I've been blocked on this issue for weeks now. I've been looking for a way to disable/enable my timer with a toggle from the main menu but I haven't manage to find a good tutorial. I'd like to let the player choose if they'd like to have a timer in-game and I'd like for the changes to be applied on all of my scenes.

    Here's my Timer's script:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6.  
    7. public class InGameTimer : MonoBehaviour
    8. {
    9.     public Text timerText;
    10.  
    11.     private float startTime;
    12.  
    13.     private bool finished = false;
    14.  
    15.  
    16.     void Start()
    17.     {
    18.         startTime = Time.time;
    19.     }
    20.  
    21.     void Update()
    22.     {
    23.         if (finished)
    24.             return;
    25.         float t = Time.time - startTime;
    26.         string minutes = ((int)t / 60).ToString();
    27.         string seconds = (t % 60).ToString("00");
    28.         timerText.text = minutes + ":" + seconds;
    29.        
    30.     }
    31.  
    32.     public void Finish()
    33.     {
    34.         finished = true;
    35.     }
    36.  
    37.     }
    The best that I have managed was by using Toggle.onValueChanged to disable my timer once the toggle is unchecked and using DontDestroyOnLoad. However, I don't want a timer on my main menu scene and Toggle.onValueChanged doesn't bring back my timer if I check back the toggle box.

    Here's the code that was used for that:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6. public class MainMenuController : MonoBehaviour
    7. {
    8. public GameObject Dontdestroy1;
    9. [SerializeField] private Toggle timerToggle;
    10.  
    11. private void Start()
    12.     {
    13. timerToggle.onValueChanged.AddListener(delegate {
    14. ToggleValueChanged(timerToggle);
    15. });
    16. }
    17.  
    18. public void PlayLevel1()
    19.     {
    20.         SceneManager.LoadScene("LEVEL1");
    21.         DontDestroyOnLoad(this.Dontdestroy1);
    22.     }
    23.  
    24. void ToggleValueChanged(Toggle change)
    25. {
    26.     GameObject.Find("Timer").SetActive(false);
    27. }
    28. }
    If anyone could bring me some insights on this, I'd be really grateful.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Well for one thing, in your ToggleValueChanged method, you should check what the current value of the toggle is (via change.isOn) — don't just assume it's been toggled off. Do the right thing when it is toggled back on, too.

    For the bigger problem, if you want your timer to be hidden in one scene (the Main Menu) but not others, and you also want it to be user-hideable, then you can't possibly do it with just the activation state of the GameObject. You're going to need an actual variable to keep track of whether the user wants a timer. Or better yet, a PlayerPrefs entry; that way it will persist trivially across scenes, and also across plays of the game, which your users will appreciate.

    So:

    1. In ToggleValueChanged, write the current on/off choice to PlayerPrefs.
    2. In your timer script, check the PlayerPrefs value and show/hide the text accordingly. (I recommend you don't actually disable the timer object, as it will need to be active in order to check the value again — which you might do several times per second or whatever.)
    3. No need for DontDestroyOnLoad anymore; it's fine for each scene to have its own instance of the timer prefab.
     
  3. Idylikos

    Idylikos

    Joined:
    Jun 13, 2022
    Posts:
    2
    Hello! Thank you for replying!

    I'm sorry to say this but I need a little more guidance since I'm new to Unity and there's a lot of things that I'm still not familiar with.

    For ToggleValueChanged, by current on/off choice to PlayerPrefs, do you mean something like this? https://answers.unity.com/questions/1541502/can-playerprefs-for-toggleison.html

    For my Timer script, what would you recommend instead of disabling my timer GameObject with SetActive(false)? I've seen some suggesting to disable the Renderer, would that be better?

    I've used PlayerPrefs for my other settings following this tutorial:
    . The only toggle element it contains is a FullScreen toggle for which the code is the following:
    Code (CSharp):
    1. public class MainMenuController : MonoBehaviour
    2. {
    3.     private bool _isFullScreen;
    4.     [SerializeField] private Toggle fullScreenToggle;
    5.  
    6.     public void SetFullScreen(bool isFullscreen)
    7.     {
    8.         _isFullScreen = isFullscreen;
    9.     }
    10.  
    11. public void GraphicsApply()
    12.     {
    13.         PlayerPrefs.SetInt("masterFullscreen", (_isFullScreen ? 1 : 0));
    14.         Screen.fullScreen = _isFullScreen;
    15.      }
    16.  
    17.     public void ResetButton(string MenuType)
    18.     {
    19.         if (MenuType == "Graphics")
    20.         {
    21.              fullScreenToggle.isOn = false;
    22.             Screen.fullScreen = false;
    23.             GraphicsApply();
    24.         }
    25.     }
    26. }
    If I try to follow this example for my timer, I get stuck at the lines with
    Screen.fullScreen
    as I don't know what code would need to be here to toggle my timer display.

    Thank you again for your insights :)