Need help with NullReferenceException

Hello Nina,
I’ve following your suggestion and so far I think I have isolated it, but honestly I don’t know how to fix it… As soon the Player dies, is supposed to call the level script (in my case the name is “scLevel” cause all my scripts begin with “sc” to avoid confusion). But the Null error points to scDamageDealer and doesn’t load de GameOver Scene. WHY?? Thanks in advance for your advice,… here are my codes.

First the Unity Console’s message:

NullReferenceException: Object reference not set to an instance of an object
scPlayer.ProcessImpact (scDamageDealer refImpacted) (at Assets/Scripts/scPlayer.cs:97)
scPlayer.OnTriggerEnter2D (UnityEngine.Collider2D enemyfire) (at Assets/Scripts/scPlayer.cs:77)

And now just part of the Player’s script where the error message points:

private void OnTriggerEnter2D(Collider2D enemyfire)
{
    scDamageDealer refimpacted = enemyfire.gameObject.GetComponent<scDamageDealer>();
    ProcessImpact(refimpacted);  // **THIS IS LINE 77**
        }

private void ProcessImpact(scDamageDealer refImpacted) //refImpacted es solo un nombre para indicar que el 
    // ProcessImpact de la linea 69
{
    
    playerHealth -= refImpacted.GetDamage();
    refImpacted.Hit();
    GameObject Explosion = Instantiate(ExplosionPrefab, transform.position, transform.rotation) as GameObject;
    Destroy(Explosion, 1f);
    AudioSource.PlayClipAtPoint(playerDeathSound, Camera.main.transform.position, playerDeathVolume);

    if (playerHealth <= 0)
    {
        gameObject.SetActive(false);
        FindObjectOfType<scLevel>().LoadGameOver(); // AND THIS IS LINE 97  HERE APPEARS THE PROBLEM**
        Die();
       
       
        //FindObjectOfType<scLevel>().LoadStartMenu();
           
        
    }
    
}

private void Die()
{
    Destroy(gameObject); //This destroy the player

}

********This is the DamageDealer script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class scDamageDealer : MonoBehaviour
{
[SerializeField] int refImpacted = 25;

public int GetDamage()
{
    
    return refImpacted;
}

public void Hit()
{
    gameObject.SetActive(false); // To disable the gameobject before is Destroyed....
                                           
    Destroy(gameObject);
}

}

*******and finally my scLevel scripts

public void LoadGameOver()
{

    SceneManager.LoadScene("Game Over");

}

Excuse me if Im not using this forum eficiently, is my first time. Thanks again

Found the solution!!

A stupid mistake> never asigned the level.sc script to the “level” game object in the hierarchy. Now everything works!

Good job on solving the issue yourself. :slight_smile:

For future reference, please feel free to create your own thread if the issue is not related to the problem the original creator of the thread has or if your problem is a bit more complex. If you followed one of my instructions, you could link them in your thread.

I’m trying to create a new thread for you now. Wish me luck. :wink:


Updated Wed Dec 02 2020 10:22
It worked. Obviously.


See also:

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms