I am working with three scripts, PlayerControl, Ammo, and Attack.
-
When a player performs an attack, isLightAttack is set to true in PlayerControl and Shoot() is executed.
-
Ammo is attached to the bullet getting sent out and attack.DealDamage(collision) is triggered.
-
DealDamage() is then called in the Attack script and Debug.Log(“Deal Damage Launched”); prints but then I get the following error.
-
Here is the error:
NullReferenceException: Object reference not set to an instance of an object
Attack.DealDamage (UnityEngine.Collider2D collision) (at Assets/Scripts/Weapons/Attacks/Attack.cs:50)
Ammo.OnTriggerEnter2D (UnityEngine.Collider2D collision) (at Assets/Scripts/Weapons/Ammo/Ammo.cs:68)
I have been trying to figure out what I could be doing wrong but have not made much progress. Any help or tips are appreciated.
PlayerControl:
public void LightAttack(InputAction.CallbackContext context)
{
if (context.performed)
{
isAttackPerformed = true;
isLightAttack = true;
isHeavyAttack = false;
animator.SetTrigger("LightAttack");
moveVector = Vector2.zero;
Debug.Log("Light damage " + lightAttackDamage);
//if (staminaBar != null)
//{
Debug.Log("Stamina Light Attack");
StaminaBar.instance.UseStamina(lightAttackStamina);
if (gameObject.tag == "PlayerRanged")
{
Shoot();
}
if (gameObject.tag == "Player")
{
StartCoroutine(ActivateAttack());
}
}
}
public void Shoot()
{
Instantiate(playerAmmo, firePoint.position, firePoint.rotation);
}
Ammo:
private void OnTriggerEnter2D(Collider2D collision)
{
Attack attack = collision.GetComponent<Attack>();
if (attack != null)
{
attack.DealDamage(collision);
}
DisableAmmo();
}
Attack:
public void DealDamage(Collider2D collision)
{
Health health = collision.GetComponent<Health>();
PlayerControl playerControl = collision.GetComponent<PlayerControl>();
Debug.Log("Deal Damage Launched");
//if (health != null)
//{
isColliding = true;
//if (playerControl != null)
//{
if (playerControl.isLightAttack)
{
Debug.Log("light attack damage");
health.TakeDamage(playerControl.lightAttackDamage);
}
if (playerControl.isHeavyAttack)
{
Debug.Log("heavy attack damage");
health.TakeDamage(playerControl.heavyAttackDamage);
}
//}
//}
}