Refactoring DRY

I've heard DRY a lot when I started learning to code in my early years. I took a some C, C++, Java, and Python courses. All of which are object oriented programming languages. One common theme that we were constantly reminded of is Dont Repeat Yourself (D.R.Y). The idea behind DRY is to no repeat code, it's really hard to be jamming out code and constantly trying to remind yourself not to repeat something. Instead of racking your brain in the moment trying to remember to not repeat a line of code... A better approach is to use what is called refactoring. Refactoring is the ability to quickly identify when and where repeated code exists and look for the best ways to remove it. If I go back into my GameBehavior script, I can see some areas where refactoring will reduce the amount of unnecessary code in the program.

In GameBehavior, there are two separate instances where we set the progress text and timescale. One when the win condition is activated and once again when the loss condition triggers. To refactor the repeated code the following function is added to GameBehavior that takes a string as an input.

public void UpdateScene(string updatedText)
    {
        ProgressText.text = updatedText;
        Time.timeScale = 0f;
    }

So the whole GameBehavior script now looks like this:

public class GameBehavior : MonoBehaviour
{
    public int MaxItems = 4;

    public Text HealthText;
    public Text ItemText;
    public Text ProgressText;
    
    void Start()
    {
        ItemText.text += _itemsCollected;
        HealthText.text += _playerHP;
    }

    // Winning condition
    public Button WinButton;
    public Button LossButton;

    public void UpdateScene(string updatedText)
    {
        ProgressText.text = updatedText;
        Time.timeScale = 0f;
    }

    private int _itemsCollected = 0;
    public int Items
    {
        get { return _itemsCollected; }
        set
        {
            _itemsCollected = value;
            ItemText.text = "Items Collected: " + Items;

            if(_itemsCollected >= MaxItems)
            {
                WinButton.gameObject.SetActive(true);
                UpdateScene("You've found all the items!");
                
            }
            else
            {
                ProgressText.text = "Item found, only " + (MaxItems = _itemsCollected) + " more to go!";
            }
        }
    }

    private int _playerHP = 3;
    public int HP
    {
        get { return _playerHP; }
        set
        {
            _playerHP = value;
            HealthText.text = "Player Health: " + HP;
            
            if(_playerHP <= 0)
            {
                LossButton.gameObject.SetActive(true);
                UpdateScene("You want another life with that?");
            }
            else
            {
                ProgressText.text = "Ouch... that's got to hurt.";
            }
            //Debug.LogFormat("Lives: {0}", _playerHP);
        }
    }
    
    public void RestartScene()
    {
        SceneManager.LoadScene(0);
        Time.timeScale = 1f;
    }
    

    // Update is called once per frame
    void Update()
    {
        
    }
}

UpdateScene is called twice in this script. It takes a string and sets the Time.timeScale to 0, effectively ending the game. When called, the string argument passed shows if the player won or lost. Win or Lose, its how you play the game.