I'm trying to make a script that gives the player a stronger attack when a powerup is walked over. It worked fine with a health increase that I made, but I cannot seem to pin down what I'm doing wrong with this one. I've been at it for the past few days and I'm not getting anywhere. The line giving the error is commented, but it can be rather difficult to see. It's the second to the last line of code:
power.damagePerShot = power.damagePerShot + DamageBoost;
I think that I grasp the nature of the error, but I do not understand why I'm receiving it. If power.damagePerShot
is taken from PlayerShooting
and then this is referenced to the correct script, I'm not understanding where the null reference is coming from.
The conponent is referenced...
The script being referenced is attached to the object...
using UnityEngine;
public class ShootPowerUp : MonoBehaviour
{
public GameObject pickupEffect;
public PlayerShooting playerShooting;
public int DamageBoost = 100;
private void Update()
{
transform.Rotate(new Vector3(15, 30, 45) * Time.deltaTime);
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
ShootPowerPickup(other);
}
}
void ShootPowerPickup(Collider player)
{
Instantiate(pickupEffect, transform.position, transform.rotation);
PlayerShooting power = player.GetComponent<PlayerShooting>();
power.damagePerShot = power.damagePerShot + DamageBoost; //This line is giving the error.
Destroy(gameObject);
}
}