5
\$\begingroup\$

I am self taught, so I'm consistently seeking ways to code better, and more efficiently. If anyone has the time, please advise me on what can be done better with explanations as to why the old method is considered inefficient, and why the new method proves to be better. Also, where I can implement more self explanatory code.

using System;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;

namespace Mans.Prototype.Lumberjack {
    [System.Serializable]
    public class GameManager : MonoBehaviour {

        public static GameManager instance { get; private set; } //singleton
        private static Timer _timer;

        [SerializeField] private GameObject _logPrefab;
        [SerializeField] private float _seconds;

        public Timer timer {
            get { return _timer; }
            set { _timer = value; }
        }
        public static GameObject logPrefab {
            get { return instance._logPrefab; }
        }

        private void Awake() {
            instance = this;
            timer = new Timer( _seconds );

            InitializeScenes();
        }
        private void Start() {
            ObjectManager.Initialize();

            UIManager.ToggleUIElement( "InGame" );
            SceneManager.SetActiveScene( SceneManager.GetSceneByName( "Level" ) );
        }
        private void Update() {
            if ( Input.GetKey( KeyCode.Return ) ) {
                Scene temp = SceneManager.GetSceneByName( "Level" );
                SceneManager.UnloadSceneAsync( temp );
                SceneManager.LoadScene( temp.buildIndex, LoadSceneMode.Additive );
            }
        }
        private void FixedUpdate() {
            if ( timer.seconds < 1f && ObjectManager.instance.treeCount > 0 ) {
                PlayerManager.instance.player.GetComponent<Animator>().enabled = false;
                UIManager.ToggleUIElement( "InGame", false );
                UIManager.ToggleUIElement( "GameOver", true );
                return;
            } else if ( timer.seconds > 0f && ObjectManager.instance.treeCount <= 0 ) {
                return;
            }
            InputManager.instance.HandleInput();

            --_timer;
        }

        private void InitializeScenes() {
            //Preload starting scenes
            LoadScene( "Scenes/UI" );
            LoadScene( "Scenes/Level 1/Level" );
            LoadScene( "Scenes/Player" );
        }
        public static void LoadScene( string path ) {
            //Loads a scene. Created so other scripts can manipulate the levels without having to include relevant namespaces each time.
            SceneManager.LoadScene( path, LoadSceneMode.Additive );
        }


        public static void MoveObjectToScene( GameObject @object, string name ) {
            //moves from current active scene to scene of choice, usually the level scene.
            SceneManager.MoveGameObjectToScene( @object, SceneManager.GetSceneByName( name ) );
        }
    }
}

I'm unsure if I have elaborated enough, if not, say so and I will update with more explanations where necessary. No specific goal, other than to improve the readability and efficiency of the code.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.