Good ol´ Nullreference Exception on Playercharacter

Hi all.

I tried a lot, but I can´t get my head around this one. It happens, as soon as the playercharacter dies and there´s bullet clones still on the screen.

I understand, that it happens, because of the player not being part anymore of the game when he dies.

But what can I do about it?

I tried some fuzzy null statements. I tried public Gameobject target instead of player (works for the exception. The projectile gets instantiated in the wrong way though…)

public class EnemyProjectile : MonoBehaviour {
    private LevelManager theLevelManager;
    private Rigidbody2D myRigidbody;
    public float speed;
    public PlayerControllerWobble player;
    public int damageToGive;
    public GameObject impactEffect;

    // Use this for initialization
    void Start () {
        theLevelManager = FindObjectOfType<LevelManager>();
        myRigidbody = GetComponent<Rigidbody2D>();
        player = FindObjectOfType<PlayerControllerWobble>();

        if(player.transform.position.x < transform.position.x)
        {
            speed = -speed;
      
        }
    }

Do I get that whole process totally wrong? If so, why?

Maybe. It will clear out some things for me… I don´t even need a corrected code. I´m also happy with a hint/tipp on how to solve this.

Thanks in advance.

What line is giving the error? How is this set up?

All three of the functions that you call in start can return null. So trying to use any of those variables when they don’t exist could be the problem.

1 Like

Thanks for the link. I´ll edit the post to fit the guideline.

Line15 if(player.transform.position.x < transform.position.x)

is causing the problem (at least console says so…)

Setup is: A “shoot at Player in Range” Script attached to the enemy and the projectile script as shown above.

public class ShootAtPlayerInRange : MonoBehaviour {

    public float playerRange;

    public GameObject enemyStar;
    private PlayerControllerWobble player;

    public Transform launchPoint;

    public float waitBetweenShots;
    private float shotCounter;


    // Use this for initialization
    void Start () {
        player = FindObjectOfType<PlayerControllerWobble>();
        shotCounter = waitBetweenShots;
    }
  
    // Update is called once per frame
    void Update () {
  
        Debug.DrawLine (new Vector3(transform.position.x - playerRange, transform.position.y, transform.position.z), new Vector3(transform.position.x + playerRange, transform.position.y, transform.position.z));
        shotCounter -= Time.deltaTime;

        if(transform.localScale.x < 0 && player.transform.position.x > transform.position.x && player.transform.position.x < transform.position.x + playerRange && shotCounter < 0)

        {
            Instantiate(enemyStar, launchPoint.position, launchPoint.rotation);
            shotCounter = waitBetweenShots;
        }
  
        if(transform.localScale.x > 0 && player.transform.position.x < transform.position.x && player.transform.position.x > transform.position.x - playerRange && shotCounter < 0)

        {
            Instantiate(enemyStar, launchPoint.position, launchPoint.rotation);
            shotCounter = waitBetweenShots;
        }
  
    }
}

This is how I detect, when to instantiate projectiles.

Maybe try adding a nullcheck for the player:

if(player != null && player.transform.position.x < transform.position.x)
1 Like

I don´t exactly know why… but apparently it worked, even though I tried something similar before? :smile:
Is the order of the statement important? Mine was like the opposite way around with the null statement in the end.

Edit: How do you mark an answer as the correct one?

Yes, the order of the statement is important. The checks are processed from left to right. If you do the transform check first it will throw an exception.

And I don’t think that the forum gives you the possibility to mark an answer as correct. But I could be wrong.

1 Like

Thank you very much for your reply. This pretty much explains, why my nullcheck didn´t work out. I´ll keep that in mind.

Kinda Important to know about that in general.

Thanks folks!