Hey everyone, I’ve been using unity for about 8 months now. I’m making a top down dungeon game with a few friends and we’ve been able to solve every problem except this one. When I pick up my item that is suppose to increase the damage of my weapon. I get this error.
NullReferenceException: Object reference not set to an instance of an object
PowerUp.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Scripts/PowerUp.cs:12)
PowerUp Script (C#);
//PowerUp Script
using UnityEngine;
using System.Collections;
public class PowerUp : MonoBehaviour {
public int addDamage;
void OnTriggerEnter2D (Collider2D other)
{
if (other.gameObject.tag == "Player") {
other.gameObject.GetComponent<attackTrigger> ().PowerUpIncrease (addDamage);
Destroy (gameObject);
}
}
}
My Attack Trigger/Damage Script (C#):
using UnityEngine;
using System.Collections;
public class attackTrigger : MonoBehaviour
{
// Sets how much damage we want to do to the enemy.
public int startDamage;
public int currentDamage;
public int maxDamage;
void Start ()
{
//Sets Starting Damage
currentDamage = startDamage;
}
void Update()
{
//Makes it so damage can't be maxed not over the intended amount
if (currentDamage>maxDamage){
currentDamage = maxDamage;
}
}
public void OnTriggerEnter2D(Collider2D other)
{
// Checks if the tag is listed as 'Enemy'.
if (other.gameObject.tag == "Enemy")
{
// Accesses the enemyDamage.cs script.
other.gameObject.GetComponent<enemyDamage> ().HurtEnemy (currentDamage);
}
}
//The called variable in the other class...
public void PowerUpIncrease(int addDamage){
//Adds additional damage to the current damage...
currentDamage += addDamage;
}
}
Enemy Damage Script (C#):
using UnityEngine;
using System.Collections;
public class enemyDamage : MonoBehaviour
{
public int MaxHealth = 100;
public int CurrentHealth;
void Start ()
{
// At the start of our game, we want the current health of the enemy to equal the max health.
CurrentHealth = MaxHealth;
}
void Update ()
{
// If the enemy's current health is less than zero...
if (CurrentHealth <= 0)
{
// Remove it from the game.
Destroy (gameObject);
}
}
public void HurtEnemy(int currentDamage)
{
CurrentHealth -= currentDamage;
}
public void SetMaxHealth()
{
CurrentHealth = MaxHealth;
}
}
The goal that I am trying to reach is when I get my power up. For the damage to increase.