Hello, I'm trying to learn how to script and encountered a null reference error. I know what it indicates, but I cannot figure out what I'm missing. I made another one that works and used its code as a template, but in doing so I created the error. The script is attached to the object that I'm referencing, which as I understand is the object that is supposed to be referenced. Could anyone please offer suggestions as to things that I could look at or even spot an oversight in the code? Code (CSharp): using UnityEngine; public class ShootPowerUp : MonoBehaviour { public GameObject pickupEffect; PlayerShooting playerShooting; public int DamageBoost = 25; 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; // [B]Null reference[/B] [B]error for this line[/B] // Debug.Log("ShootPowerPickup"); Destroy(gameObject); } }
that error is saying that the power variable which is your PlayerShooting script, is null. Something is going wrong on your player.GetComponent<PlayerShooting>(); line. Make sure the player object has the PlayerShooting script on it.
Nope it would only be the player object or the GetComponent call, as you're not referencing the pickupEffect object.