Update() not working for some reason

I am currently working on a simple project where I’m finally putting all (or most) of my knowledge to use. I have two base classes, one called Character.cs and the other one CharacterAttack.cs (the names are self-explanatory)

For some reason, the Update() function inside of CharacterAttack doesn’t work. I tried putting a Debug.Log to see, whether or not it’s even running but that proved to not be the case.

Character.cs:

using UnityEngine;

public class Character : MonoBehaviour
{
    public CharacterStats charData = null;

    public int currentHP = 0;

    void Start()
    {
        if(!charData)
        {
            charData = ScriptableObject.CreateInstance<CharacterStats>();
            Debug.LogWarningFormat("Character Data on {0} not present! Created a default instance.", gameObject.name);
        }

        currentHP = charData.maxHP;
    }

    void Update()
    {
        print("Character class' Update on " + gameObject.name + " is running");

        if (currentHP <= 0)
        {
            Die();
        }
    }

    public virtual void TakeDamage(int damageAmount)
    {
        currentHP -= damageAmount;
        Debug.LogFormat("{0} taken damage! Previous HP: {1} | Current HP: {2} | Damage Amount: {3}", 
                            gameObject.name, currentHP + damageAmount, currentHP, damageAmount);
    }

    public virtual void Die()
    {
        Destroy(gameObject);
        Debug.LogFormat("{0} died!", gameObject.name);
    }
}

CharacterAttack.cs:
using UnityEngine;

public class CharacterAttack : MonoBehaviour
{
    public WeaponStats weaponStats = null;
    [Space]
    public bool startCooldown = false;

    private float _attackCooldown;
    private float _attackCooldownInitial;

    void Start()
    {
        if(!weaponStats)
        {
            weaponStats = ScriptableObject.CreateInstance<WeaponStats>();
            Debug.LogWarningFormat("Weapon Data on {0} not present! Created a default instance.", gameObject.name);

            _attackCooldown = weaponStats.attackCooldown;
            _attackCooldownInitial = weaponStats.attackCooldown;
        }
    }
    
    void Update() //-- ISSUE HERE
    {
        print("CharacterAttack class' Update on " + gameObject.name + " is running");

        if (startCooldown)
        {
            Debug.Log("Cooldown started for " + gameObject.name);

            _attackCooldown -= Time.time;

            if(_attackCooldown <= 0)
            {
                Debug.Log("Cooldown stopped for " + gameObject.name);

                startCooldown = false;

                _attackCooldown = _attackCooldownInitial;
            }
        }
    }

    public virtual void Attack(int damageAmount, GameObject objectToAttack) { startCooldown = true; print(gameObject.name + " | " + startCooldown); }
}

Console image:
110495-dammit.png

Any and every help is appreciated.

If you have subclasses of CharacterAttack, then :

  1. CharacterAttack.Update must be marked as virtual
  2. EnemyAttack.Update and PlayerAttack.Update must be marked as override and you must call base.Update() in these functions.

An update function on a base class will not run on a derived class. You need to change the name to something like protected void UpdateCalls and call that method in the update on the derived class

Hello,

I may sound dumb but in your scene, is your script (CharacterAttack.cs) attached to an active GameObject ? if not, that’s the problem.

If yes, there might be a problem in your start function or else…

Waiting your answer :slight_smile: