The Problem of Loading next Scene

Hello Everyone.

I got a small problem when I load to the next scene.
Instead of loading the “game over” scene for all situations, I created two scenes.

1)The Scenes of Win
Load to it once the point reaches 1000 points.

  1. The Scenes of Gameover
    Load to it once the HP of the player goes to 0

However, I keep having this bug occurs.
Bug

And it makes my “return main menu” button failed.
MainMenu

My code is almost the same as the RIck.
Except I added a Win Point at Update that can make the system load the win scenes once the scores reach the point I set.

Does anyone know what is the reason for this bug?
And how can I solve this?

Thank you for your help in advance.

Hi Lai,

Welcome to our community! :slight_smile:

What bug do you mean? What is the expected result? All your scenes are in the scenes list in File > Build Settings, aren’t they?

If you suspect that the code is not working, add Debug.Logs to see what’s going on during runtime.

Hi Nina,

Thank you for your help.

Here is my build setting.
I believe I set all my scenes well and properly.

When I load my Win Scene, which it loads when the scores reach 1000 points, the Hierarchy is as the below pics shown. There are two " Win Scene’ and the gray one just keep flashing and showing “is loading”. It also makes my UI-Button failed to use

1

And my code is almost as same as Rick. Except I added a Win Point at Update that can make the system load the win scenes once the scores reach the point I set.

Here is the codes.

    [SerializeField] int WinScorePoint = 1000;
    int CurrentScore = 0;
    Level level;
    
    // Start is called before the first frame update
    void Awake()
    {
        SetUpSingleton();
        level = FindObjectOfType<Level>();
    }

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

    }

    public int GetScore()
    {
        return CurrentScore;
    }
    public void AddScore (int screPoint)
    {
        CurrentScore += screPoint;
    }

    public void ResetGame()
    {
        Destroy(gameObject);
    }

    public void Update()
    {
        if (CurrentScore> WinScorePoint)
        {
            level.LoadWinScene();
        }

    }

I’m wondering if the issue is caused by the Update method which might call LoadWinScene multiple times. Have you already tried to add Debug.Logs to your code to see what is going on during runtime?

Try to move the code from Update do the AddScore method.

Hi Nina,

Thank you so much for your help.
Yes, you are right. It was a simple mistake that I got confused at using the Update method :sweat_smile:. The problem was easily solved by changing the codes to another method. Thank you so much.

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

Privacy & Terms