- Home /
Damager Powerup issue - NullReferenceException: Object reference not set to an instance of an object
Hi all, I am currently creating a damage powerup, so when my player collides with an object it will increase the damage essentially. I have used this method for other powerups but for some reason it is not linking this one.
My powerup code is
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class damagePowerup : MonoBehaviour {
public int damageIncrease = 10;
void Update () {
gameObject.transform.Rotate (new Vector3(0, 30, 0) * Time.deltaTime);
}
public void OnTriggerEnter(Collider other) {
if (other.tag == "Player") {
Debug.Log("Damage Powerup");
PlayerShooting NewDamage = other.gameObject.GetComponent<PlayerShooting> ();
NewDamage.damagePerShot = damageIncrease;
transform.position = new Vector3(10000, 10000, 100000);
StartCoroutine ("WaitTime", NewDamage);
}
}
public IEnumerator WaitTime(PlayerShooting playerDamage) {
yield return new WaitForSeconds (5);
playerDamage.damagePerShot = 100;
Destroy (gameObject);
}
}
and my PlayerShooting script that I am accessing is
using UnityEngine;
public class PlayerShooting : MonoBehaviour
{
public int damagePerShot = 20;
public float timeBetweenBullets = 0.15f;
public float range = 100f;
float timer;
Ray shootRay;
RaycastHit shootHit;
int shootableMask;
ParticleSystem gunParticles;
LineRenderer gunLine;
AudioSource gunAudio;
Light gunLight;
float effectsDisplayTime = 0.2f;
void Awake ()
{
shootableMask = LayerMask.GetMask ("Shootable");
gunParticles = GetComponent<ParticleSystem> ();
gunLine = GetComponent <LineRenderer> ();
// gunAudio = GetComponent<AudioSource> ();
}
void Update ()
{
timer += Time.deltaTime;
if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0)
{
Shoot ();
}
if(timer >= timeBetweenBullets * effectsDisplayTime)
{
DisableEffects ();
}
}
public void DisableEffects ()
{
gunLine.enabled = false;
}
public void Shoot ()
{
timer = 0f;
// gunAudio.Play ();
gunParticles.Play ();
gunLine.enabled = true;
gunLine.SetPosition (0, transform.position);
shootRay.origin = transform.position;
shootRay.direction = transform.forward;
if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
{
EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
if(enemyHealth != null)
{
enemyHealth.TakeDamage (damagePerShot, shootHit.point);
}
gunLine.SetPosition (1, shootHit.point);
}
else
{
gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
}
}
}
The error I am getting when playing the game is NullReferenceException: Object reference not set to an instance of an object damagePowerup.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/damagePowerup.cs:18)
I do not understand why it doesn't like this line of code as I am accessing the script, getting the variable I want and added a new value?
If someone could help me out that would be great!
You are getting a reference error for line 18, which is probably related to line 17, there might not be a PlayerShooting component on other.gameObject
I think you will run into some behavioral problems when you fix this though, on line 18 you are assigning the damage variable to be 10 even though the default damage is 20, which would mean you do less damage on the powerup. In line 19, you make the powerup go way offscreen ins$$anonymous$$d of destroying it?
On line 26 of your coroutine, after 5 seconds pass the damage then goes from 10 to 100, so the damage doesn't go back to normal it just goes incredibly high.
Thankyou! You made it click in my brain that the playerShooting script was actually on a child element, plain sailing from there. The values were just random while testing, however all is good now, again, thankyou :)
Answer by bradarnott · Dec 08, 2016 at 10:05 AM
PlayerShooting NewDamage = other.gameObject.GetComponentInChildren<PlayerShooting> ();
Was needed, due to the script being on a child element of the player
Answer by edcx · Dec 08, 2016 at 07:07 AM
It's actually telling you what the error is! All you need to do is look what those variables are at that line. What I can tell from here is that your "NewDamage" variable is null. And the reason it's being null is, the other object you are getting from trigger does not have "PlayerShooting" component.
Your answer
Follow this Question
Related Questions
Powerup issue - Speed 1 Answer
How do Coroutines behave with Triggers? 1 Answer
Coroutine doesn't work when called from another method. 3 Answers
Issue With Simultaneous ReadPixels Calls 0 Answers