Problem with destroying gameStatus when I restart my game

Well i just thought to try some other way than it was taught in the lesson to delete the gameStatus gameobject when we reset our. I wrote:-

public void LoadStatingScene()
{
      SceneManager.LoadScene(0);
      Destroy(FindObjectOfType<GameStatus>());   //tried to use my mind but...
}

I thought it will find the GameStatus gameobject and destroyed it…but nothing happened…instead something strange happened…every time I load the starting screen a new GameStatus get formed(this continues every time I reset the game)

Plz let me know why this dosen’t work and why does everytime a new GameStatus get formed…

Hi Avhinav,

Each game object in your Hierarchy gets created when the scene loads. You cannot prevent this from happening because that’s how Unity works.

When you call LoadScene in a method, it might be that the rest of the method code block does not get executed anymore. Maybe you could try to swap the two lines in your LoadStatingScene if you suspect that the issue occurs because the GameStatus did not get destroyed.

Well I swaped the line of LoadScene and Destroy(FindObjectOfType<GameStatus>());, however the same thing happened. ik this way of writing is incorrect but why?? i want to know :thinking:

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.

Nah, nothing happened.

Here’s a reference clip for u about what is happening:-

https://drive.google.com/file/d/17OJFHn_Se_V5ALIRfI5snIJ9yjEUzvuc/view?usp=sharing

And my code lines:-

Thank you for the video. It was helpful.

Do(es) the Game Status game object(s) in the scene(s) have got a GameStatus script attached?

And could you share your GameStatus class as formatted text here?


See also:

Here’s my game status script:-

public class GameStatus : MonoBehaviour
{
    [Range(0, 10)] [SerializeField] private float gameSpeed = 1f;
    [SerializeField] private int scoreToBeAdded = 15;
    [SerializeField] private int currentScore;
    [SerializeField] TextMeshProUGUI scoreText;

    private void Awake()
    {
        int noOfGameStatus = FindObjectsOfType<GameStatus>().Length;

        if (noOfGameStatus > 1)
        {
            gameObject.SetActive(false);
            Destroy(gameObject);
        }
        else
        {
            DontDestroyOnLoad(gameObject);
        }
    }

    private void Start()
    {
        scoreText.text = currentScore.ToString();
    }

    void Update()
    {
        Time.timeScale = gameSpeed;
    }

    public void Score()
    {
        currentScore += scoreToBeAdded;
        scoreText.text = currentScore.ToString();
    }

    public void RestartGame()    // To destroy GameStatus when we restart the game
    {
        Destroy(gameObject);
    }
}

Well I can use FindObjectOfType<GameStatus>().RestartGame(); it in my LoadStatrtingScene() and it works all fine, but why Destroy(FindObjectOfType<GameStatus>()); dosen’t work?? :woozy_face:

Good question. :confused:

Try this:

public void RestartGame()    // To destroy GameStatus when we restart the game
{
    Debug.Log(GetInstanceID() + " --- RestartGame() was called.");
    gameObject.SetActive(false);
    Destroy(gameObject);
}

You could add a similar to the Start method to see what instance id your persistent game object has. Maybe the RestartGame method of the wrong GameStatus instance gets called during runtime.

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

Privacy & Terms