Hi. I’ve been using Unity for a couple of months now and since I have no previous experience in coding then I’ve just gone with simple solutions which I’ve picked up from tutorials and which work for me. Recently I’ve read about clean code, so I thought maybe someone could give me advice with my simple code, like what to change and what not to use or to etc. Following is a little script form my infinite jumper game which controls the game over sequence. Any advice is appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Game_Over_Controller : MonoBehaviour
{
private GameObject[] gameOverObjects;
public static bool isGameOver;
[SerializeField] private Transform playerTransform;
[SerializeField] private float fallDistanceToGameOver;
private float playerHighestPoint;
[SerializeField] private Text currentScoreText;
[SerializeField] private Text highscoreText;
// Start is called before the first frame update
void Start()
{
isGameOver = false;
gameOverObjects = GameObject.FindGameObjectsWithTag("Game_Over_Objects");
HideGameOverObjects();
playerHighestPoint = playerTransform.position.y;
}
// Update is called once per frame
void Update()
{
GameOver();
}
private void HideGameOverObjects()
{
foreach (GameObject g in gameOverObjects)
{
g.SetActive(false);
}
}
private void ShowGameOverObjects()
{
foreach (GameObject g in gameOverObjects)
{
g.SetActive(true);
}
}
private void GameOver()
{
if (playerHighestPoint < playerTransform.position.y) playerHighestPoint = playerTransform.position.y;
if (!isGameOver && playerHighestPoint - playerTransform.position.y > fallDistanceToGameOver)
{
isGameOver = true;
DisplayScore();
ShowGameOverObjects();
Time.timeScale = 0;
}
}
private void DisplayScore()
{
currentScoreText.text = "Height: " + Score_Controller.currentScore + " m";
highscoreText.text = "Personal Best: " + Score_Controller.highscore + " m";
}
}