[Not Solved] object becoming null for no apparent reason?

Ok, so this might be a little long, but I’ll try to cut up the code to be as quick as i can. The quick and dirty is this: between debug.Log “start here”(2nd code block, line 24) and “Null here” (1st code block, line 27) my player object goes from being a player to being Null. I’m stumped.

On the “Start here” the debug line returns with “Start Here! Player (Player) is still good and of type ‘Player’ here.”
On debug logs “1” through “8” is returns this: “1” or “3” or “8” (depending on the line). whereas it should say “# Player (Player)”.

Which of course means that as soon as i try to do anything at all after the “8” debug, such as “ShowLifeCount(extraLives);”, I get a NullReferenceException.

Please help me find the gremlins.

Here are the relevant code snippets:

public class GameManager : MonoBehaviour
{
    public static GameManager gameManager = null;
    public Player player;


private void Awake()
    {
        if (gameManager == null)
        {
            gameManager = this;
        }
        else if (gameManager != this)
        {
            Destroy(gameObject);
        }
        DontDestroyOnLoad(gameObject);
    }

private void Start()
    {
        player = GameObject.FindWithTag("Player").GetComponent<Player>();
    }

    public void LoseOneLife()
    {
        Debug.Log(player + " is Null here");
        extraLives--;
        ShowLifeCount(extraLives);
        CheckIfGameOver();
    }
}
public class Projectile : MonoBehaviour
{
    private Player player;
    private GameManager gameManager;

private void Start()
    {
        gameManager = FindObjectOfType<GameManager>();
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        CollideWithPlayer(collision);
    }

    private void CollideWithPlayer(Collider2D other)
    {
        if (other.gameObject.CompareTag("Player"))
        {
            player = other.gameObject.GetComponent<Player>();

            if (player != null)
            {
                Debug.Log("Start Here! " + gameManager.player + " is still good and of type 'Player' here.");
                player.TakeDamage(projectileDamage);
            }
            else
            {
                Debug.Log("player is null");
            }

            Destroy(gameObject);
        }
    }
}
public class Player : MonoBehaviour
{
    private GameManager gameManager;

private void Start()
    {
        gameManager = FindObjectOfType<GameManager>();
    }

public void TakeDamage(int damageTaken)
    {
        Debug.Log("1 " + gameManager.player);

        if (vulnerable)
        {
            Debug.Log("2 " + gameManager.player);

            myHealth -= damageTaken;

            Debug.Log("3 " + gameManager.player);

            CheckDeath();
        }
    }

private void CheckDeath()
    {
        Debug.Log("4 " + gameManager.player);

        if (myHealth <= 0)
        {
            Debug.Log("5 " + gameManager.player);

            AudioSource.PlayClipAtPoint(deathSound, transform.position, 1f);

            Debug.Log("6 " + gameManager.player);

            gameObject.SetActive(false);

            Debug.Log("7 " + gameManager.player);

            gameManager.LoseOneLife();
        }
    }
}

Sure, gameObject.setActive(false) in ‘Player’. :slight_smile:

nope, I’ve tried commenting that out and i still get the error, also if it were that easy, why would i be here on the forums? =)

Next contestant?

I tried your code. not getting any problems.
However, it’s worth noting that you have a semi odd setup for a static GameManager and its variable ‘player’ , but you also reproduce references to GameManager and player.

I remade it almost identical to you, minus code functions you didn’t show.
in gamemanager’s LoseOnLife, I got player isn’t null.
So, I’m not sure if there is an issue somewhere not shown or something else. :slight_smile:

Why do you make a static instance of gamemanager and then look for it in the scripts which use it?

For your gamemanager, you’d be better off creating a singleton pattern if you’re doing it that way. In your code you’re creating a static instance of it but still accessing the script through a variable anyway?

public class GameManager : Monobehaviour
{
    private static GameManager gameManager;
    public static GameManager Instance()
    {
        if(!gameManager)
        {
            gameManager = FindObjectOfType (typeof(GameManager)) as GameManager;
        }
        return gameManager;
    }
}

Then access it from other scripts as follows

GameManager.Instance().myGameManagerMethod;

That sounds like what I’m trying to do, but obviously I’m not implementing it correctly. Could you explain a little more in detail about how to use the singleton setup you wrote down? I’ve felt for a little while now that I was doing this wrong, but I don’t think my brain is quite getting the right way to do a singleton. Or, possibly the sources I have tried to learn it from were not as reliable/knowledgeable as i thought.

So far, I’ve been having to do the whole “gameManager = FindObjectOfType();” in each .cs file that needs to access the gameManager even though i made it static in it’s own script. A lot, if not all, of the objects that these scripts are on are prefabs and I’m using instantiated/cloned versions of them, so i can’t just set it to public and drag the object into the slot via the editor (at least, i cant seem to make that work either).

Because I’m apparently doing it wrong, hence the asking for help. =)

public class GameManager : MonoBehaviour
{
    public static GameManager instance;
    public Player player;
private void Awake()
    {

         instance = this;
    }      

}

This should fix your null error issue.