Hello, so I’m not the most “advanced” programmer as I’m teaching myself but I am working on a project and I’m currently attempting to design a combat system. I created an empty game object and placed a script on it for tracking how combat damage is handled. What I was looking to do is create this script and then have all my heroes use the script to take damage. I created a reference to the combat script on my heroes script expecting it to work and my character would take damage using the update function and an input.getkeydown. However it isn’t working and I’m receiving a NullReferenceException anyone know why and how to fix this? I’ve provided both scripts below.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CombatManager : MonoBehaviour
{
public Heroes heroes;
float armorResistance;
float magicResistance;
public void Awake()
{
heroes = GetComponent<Heroes>();
}
public void TakeDamage(float attackDMG)
{
heroes.maxHealth -= heroes.attackDMG;
armorResistance = 100 / (100 + heroes.armor);
if (heroes.armor > 0)
{
heroes.maxHealth -= heroes.attackDMG * armorResistance;
Debug.Log("takes dmg");
}
}
public void MagicDamage(float abilityPOWER)
{
heroes.maxHealth -= heroes.abilityPOWER;
magicResistance = 100 / (100 + heroes.magicResist);
if(heroes.magicResist > 0)
{
heroes.maxHealth -= heroes.abilityPOWER * magicResistance;
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Heroes : MonoBehaviour
{
public Hero hero;
private CombatManager combatManager;
public float maxHealth;
public float mana;
public float attackDMG;
public float abilityPOWER;
public float armor;
public float magicResist;
public float attackSpeed;
public float cooldown;
public float critical;
void Start()
{
maxHealth = hero.Health;
mana = hero.mana;
attackDMG = hero.attackDmg;
abilityPOWER = hero.abilityPower;
armor = hero.armor;
magicResist = hero.magicResist;
attackSpeed = hero.attackSpeed;
cooldown = hero.cooldown;
critical = hero.criticalStrike;
combatManager = GetComponent<CombatManager>();
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.J))
{
combatManager.TakeDamage(200);
}
}
void PlayerGrowth()
{
}
}