How should I go about optimizing my State handler in order to avoid bloat?

I am a game programming student early on I learned about the difference between pausing games with Time.timeScale and a singleton state machine. I really liked learning how to use a singleton and implementing them in my projects.
The only issue with it is that if I want to pause the gravity, animation and stuff like that (essentially anything other than stopping the movement itself )I have to implement them individually.

What I want to do is instead update my state machine to check if the object contains anything that would need an additional check. As right now calling pause can potentially be quite bloated. I will post the state machine as well as a chunk of bloated code.
Stuff like the gameover enum and cases are commented out since they were not needed for my most recent project.

#region GameStateHandler
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
/* 
 This is a gamestate handler that uses the singleton design pattern. Its primary function for this project is for pausing and resuming from pause. 
 I chose to use a state machine for handling the pause functionality in our game due to the fact that it enables a larger amount of 
 control than in comparison to something like TimeScale = 0.
 
 In order for something to be paused a reference to this script would be called like so: 
    
    if (GameStateHandler.Instance != null && GameStateHandler.Instance.CurrentState == GameState.Paused)
        {
            return;
        }

 This will stop the object once was pause has been activated.   
 */

public enum GameState
{
    Playing,
    Paused,
    //GameOver
}

public class GameStateHandler : MonoBehaviour
{
    public static GameStateHandler Instance { get; private set; }

    public GameState CurrentState { get; private set; } = GameState.Playing;

    public Animator[] animators;

    public Health health;

    private PlayerMovement playerMovement;

    public static bool Paused;
    public bool isGameOver = false;

    private UIManager uiManager;

    //While Asynchronistic loading isn't exactly necessary for handling pause/playing state I have included it to ensure smooth loading between the menu and levels.

    public void SceneChange(string name)
    {
        StartCoroutine(LoadSceneAsync(name));
    }

    private IEnumerator LoadSceneAsync(string sceneName)
    {
        AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(sceneName);

        while (!asyncLoad.isDone)
        {
            yield return null;
        }
    }

    void Awake()
    {
        if (Instance == null)
        {
            Instance = this;
        }
        else
        {
            Destroy(gameObject);
        }

        uiManager = GetComponent<UIManager>();

        if (uiManager == null)
        {
            Debug.Log("UIManager not found in scene, please attach.");
        }
    }

    void Start()
    {
        if (uiManager != null)
        {
            uiManager.OnResumePress();
        }
    }

    public void ChangeState(GameState newState) //Handlles the transition between states. 
    {
        if (CurrentState == newState) return;
        CurrentState = newState;

        switch (newState)
        {
            case GameState.Paused:
                PauseGame();
                break;
            case GameState.Playing:
                ResumeGame();
                break;
                //case GameState.GameOver:
                //    GameOver();
                //    break;
        }
    }

    public void TogglePause()
    {

        if (CurrentState == GameState.Playing)
        {
            ChangeState(GameState.Paused);
        }
        else if (CurrentState == GameState.Paused)
        {
            ChangeState(GameState.Playing);
        }
    }
    public void PauseGame()
    {
        Debug.Log("Game Paused");

        //stop animations
        foreach (Animator animator in animators)
        {
            animator.enabled = false; //pauses all animations
        }
    }

    public void ResumeGame()
    {
        if (uiManager != null)
        {
            uiManager.OnResumePress();
        }
        Debug.Log("Game Resumed");

        ChangeState(GameState.Playing);
    }
    public void GoToMainMenu()
    {
        if (GameStateHandler.Instance.CurrentState == GameState.Paused)
        {
            return;
        }
        SceneManager.LoadScene(0); //Assuming menu = scene 0
    }

    public void QuitGame()
    {
        Application.Quit();
        Debug.Log("See ya later!");
    }

    //The Game Over state ended up not being used in this project. 

    //public void GameOver()
    //{
    //    if (CurrentState == GameState.Paused)
    //    {
    //        ResumeGame();
    //    }

    //    ChangeState(GameState.GameOver);
    //    isGameOver = true;
    //    Debug.Log("Game Over");

    //    if (playerMovement != null)
    //    {
    //        playerMovement.enabled = false;
    //    }

    //    Paused = false;
    //}
}
#endregion 
    void Update()
    {
        if (GameStateHandler.Instance != null && GameStateHandler.Instance.CurrentState == GameState.Paused)
        {
            if (ani != null && ani.speed != 0)
            {
                previousAnimatorSpeed = ani.speed;
                ani.speed = 0;
            }

            if (rb.useGravity)
            {
                isGravityEnabled = true;
                rb.useGravity = false;

                previousLinearVelocity = rb.linearVelocity; //store previous velocity otherwise you can spam pause and keep going forward
                rb.linearVelocity = Vector3.zero; // Stop all movement
            }

            transform.position = previousPosition;
            return;
        }
        else
        {
            // Restore gravity when resumed
            if (!isGravityEnabled && rb.useGravity == false)
            {
                rb.useGravity = true;
            }

            // Restore animation speed when resumed
            if (ani != null && ani.speed == 0 && previousAnimatorSpeed != 0)
            {
                ani.speed = previousAnimatorSpeed;
            }

            //restore velocity when resumed otherwise you can spam pause and keep going forward
            if (rb.linearVelocity == Vector3.zero)
            {
                rb.linearVelocity = previousLinearVelocity;
            }
        }
        {
            rb.useGravity = isGravityEnabled;

            if (ani.speed == 0 && previousAnimatorSpeed != 0)
            {
                ani.speed = previousAnimatorSpeed;
            }
        }

        previousPosition = transform.position; ```


I haven't tried anything as of now however I am thinking that if the game state handler included all the relevant variables (animation, gravity etc) then it could check if the script its being called has them and in that case it would pause all relevant fields.  

New contributor

Indra Cosimo Bowen-Paoli is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật