Enemy not destroying, and not updating the score

I followed in q and a nina’s help in there but still not working
prtsc/f11 broken

Game score:

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

public class GameScore : MonoBehaviour
{

int score = 1;

private void Awake()
{
    SetUpSingleton();
}

private void SetUpSingleton()
{
    int numberOfGameSessions = FindObjectsOfType<GameScore>().Length;
    if (numberOfGameSessions > 1)
    {
        Destroy(gameObject);
    }
    else
    {
        DontDestroyOnLoad(gameObject);
    }
}

public int GetScore()
{
    return score;
}

public void AddToScore(int scoreValue)
{
    score += scoreValue;
}

public void ResetGame()
{
    gameObject.SetActive(false);
    Destroy(gameObject);
}

}

Score Display:

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

public class ScoreDisplay : MonoBehaviour
{

Text scoreText;
GameScore score;

// Start is called before the first frame update
void Start()
{
    scoreText = GetComponent<Text>();
    score = FindObjectOfType<GameScore>();
}

// Update is called once per frame
void Update()
{
    scoreText.text = score.GetScore().ToString();
}

}

ERROR:

NullReferenceException: Object reference not set to an instance of an object

Thank you!!!

Hi,

Check if your “singleton” calls gameObject.SetActive(false); in the same if-block where Destroy(gameObject); gets called. If there isn’t that line of code, add it.

Did this help you fix the problem?


See also:

nina i added it but still not working

GameScore:

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

public class GameScore : MonoBehaviour
{

int score = 1;

private void Awake()
{
    SetUpSingleton();
}

private void SetUpSingleton()
{
    int numberOfGameSessions = FindObjectsOfType<GameScore>().Length;
    if (numberOfGameSessions > 1)
    {
        gameObject.SetActive(false);
        Destroy(gameObject);
    }
    else
    {
        DontDestroyOnLoad(gameObject);
    }
}

public int GetScore()
{
    return score;
}

public void AddToScore(int scoreValue)
{
    score += scoreValue;
}

public void ResetGame()
{
    gameObject.SetActive(false);
    Destroy(gameObject);
}

}

Bruh…

i was missing something
the add empty then game score

welp… case closed

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

Privacy & Terms