Using classes and constructors as methods?

Right now my scripts are very messy and try to do everything at once, as soon as I fix one error or optimize one situation, another error pops up and it interferes with optimization of a different situation.

I watched the Unity video on Classes and they say the point of OOP is to break down tasks into single scripts. So I started with the “UpdatePlayerHealth” in my “BattleHUD” script.

It happens the constructor is a method in of itself, I found the only way I can call this in another script… i.e. my enemy script after the attack… is to put

new UpdatePlayerHealth(player1, player1text);

does using the “new” keyword create a whole new “object” / “instance” of that class and use up memory?? would it be better to make a general “BattleHUDHelperFunctions” class/script and call the methods from in there??

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UpdatePlayerHealth{
    public UpdatePlayerHealth(Player p, Text php){
        if(p.HP < 0){p.HP = 0;}
        if (p.HP > p.MaxHP) {
            p.HP = p.MaxHP;
        }
        php.text = p.HP.ToString ();
    }
}

this means I have to make my scripts static? is static a “bad” thing?

Yes, it means you’re creating a new instance.

Could be…

Though usually you would have a ‘Health’ script, and a ‘UpdateHealth’ method on the Health script.

Static isn’t necessarily bad, you just need to use it correctly.

For instance… in your current example I’d separate out the Health and the View/Display. It’d be something more like this:

public class HealthMeter : MonoBehaviour
{
   
    [SerializeField]
    private float _health;
   
    [SerializeField]
    private float _maxHealth;
   
   
    public float Health
    {
        get { return _health; }
        set { _health = Mathf.Clamp(value, 0f, _maxHealth); }
    }
   
    public float MaxHealth
    {
        get { return _maxHealth; }
        set
        {
            _maxHealth = Mathf.Max(0f, value);
            _health = Mathf.Clamp(value, 0f, _maxHealth);
        }
    }
   
}



public class HealthDisplay : MonoBehaviour
{
   
    [SerializeField]
    private Text _txt_health;
   
    public void MoveHealthToScreen(float health)
    {
        _txt_health.text = health.ToString();
    }
   
}

The job of ‘Health’ is separated from the job of displaying health.

Furthermore health is separated from ‘Player’. A Player might HAVE a HealthMeter, but it doen’t necessarily have to define the HealthMeter. What about a Mob? Wouldn’t it also have a HealthMeter? Now you’re implementing the HealthMeter in 2 disparate locations.

That’s the idea of separating out tasks into separate scripts.

The methods related to those tasks are then associated with that class/type.

Thanks, but wouldn’t this require tons of object/script referencing vs putting everything in one script?? Unless you use inheritance? would the inheritance order be something like?
Player
Health:Player
HealthMeter:Player

sorry,
Player:MonoBehaviour
Health: Player
HealthMeter: Health

no…

Because HealthMeter isn’t a Player. HealthMeter is its own thing. A Player could have a HealthMeter, a Mob could have a HealthMeter, a destructible box could have a HealthMeter.

Also… what’s ‘Health’? What would ‘Health’ implement that ‘HealthMeter’ doesn’t have?

Lastly, inheritance isn’t that important. If you have huge massively long inheritance chains like that… your design may have gotten out of hand.

One should favor composition before inheritance. The component design Unity is based on, is a composition based design pattern. Where you add components to an object to add functionality.

I’m going to take in what you said, and start from scratch.