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:
Any and every help is appreciated.