Why do I keep getting this error?
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.
Throw a debug in your powerup script to see if you're getting the attackTrigger component.
if (other.gameObject.tag == "Player") {
var atkTrig = other.gameObject.GetComponent<attackTrigger> ();
if(atkTrig == null) Debug.LogWarning("No AttackTrigger Found");
else atkTrig.PowerUpIncrease(addDamage);
Destroy (gameObject);
}
It may be that your "Player" enters the trigger, but your "Player" isn't the collider that has the attackTrigger component. Is it a child of the player? Does the actual GameObject that has the attackTrigger component and the collider have the tag "Player"? Double-check these things.
Answer by nbaylis · Mar 11, 2016 at 04:08 PM
After rechecking my tags to verify, I realized that the object that has the attackTrigger component attached to it is neither child nor a parent. It is a projectile that I spawn in another script. The attackTrigger script is just to clarify the damage that the spawned object will deal. How would I call the script on the object?
Have your projectile instantiation script (the gun) add the increased damage to the projectile, have the powerup change a damage value in the gun script.
eg :
PowerUp -> ontriggerEnter -> if player, get Gun script, Gun.extraDamage += PowerUp.damageIncrease
Gun -> shoot : instantiate Projectile -> Projectile.currentDamage += Gun.extraDamage
Thank you so much. It works now. How do I make your answer correct?