0

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 PlayerShootingand 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);
    }
}
Amy Walker
  • 35
  • 2
  • Please wait @sujith karivelli this dublicate is way over referenced and in this case doesn't answer the question at all ... Just because the OP mentions a `NullReferenceException` doesn't mean he doesn't know what it is .. he wants to know where it comes from in his case and how to solve it! – derHugo Oct 16 '18 at 05:03
  • @derHugo I wouldn't say that it's over referenced, but it's not the correct duplicate in this instance. I've voted to re-open on that basis (and on the basis that you've provided an answer). – ProgrammingLlama Oct 16 '18 at 05:06
  • @John thanks. What I mean with over referenced is that I see so many Unity specific questions mentioning somewhere `NullReferenceException` Bering marked as duplicate though in many cases this doesn't help them in any way solving their problem. I just want to be fair to new users and actually address their problem. If there is a better question with an adequate answer that at least points in the correct direction than it is a valid duplicate and go for it :) – derHugo Oct 16 '18 at 05:11
  • 2
    @John: If he understood what is null reference means he must know the answer for *why Unity was returning null*, isn't it? – sujith karivelil Oct 16 '18 at 05:11
  • @sujithkarivelil No not neccessary .. he knows than that apparently his reference has the value `null` when trying to acces it but **why** is not explained by this (see his comment on my answer) – derHugo Oct 16 '18 at 06:10

1 Answers1

2

The component PlayerShooting is not attached to the Player GameObject but one of its child's

Instead use GetComponentInChildren like

PlayerShooting power = player.GetComponentInChildren<PlayerShooting>();

and you should be fine.

derHugo
  • 83,094
  • 9
  • 75
  • 115
  • Oh my god, thank you! I did not know that GetComponentInChildren<> existed. I was under the impression that there was no distinction. – Amy Walker Oct 16 '18 at 06:11
  • You are welcome! Note that there is also `player.GetComponentInChildren(true);` which is required to find your component in the case that your component or the GameObject might be disabled at any time. – derHugo Oct 16 '18 at 06:22
  • If this solved your question feel free to accept the answer ;) – derHugo Nov 07 '18 at 20:10