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!