Can anyone tell me what the issue is with my code? (C#)

Hi guys,

I’m having a problem figuring out what exactly is wrong with my script. Basically what I need to do is have it so after the player receives points, that it will load the next stage (a scene number that I’ve put in the inspector).

All it does now though is refreshes the current level and does not give it time to give the player the points or even show that the player earned them before it makes a dash to refreshing it.

I’ll just post the code and hopefully you’ll be able to see the mistakes that I can’t.

using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;

public class Player : MonoBehaviour
{
    public int sceneNumber = 0;

    bool Moving;
    bool halfMoving;

    //bool Goal = false;
    bool Outer = false;
    bool Middle = false;
    bool Center = false;
    bool Finished = false;
    bool Ended = false;

    void Update()
    {

        if (!halfMoving && Outer)
        {
            Finished = true;
            Outer = false;
            ManageScore.score += outerPoints;
            Ended = true;
        }

        if (!halfMoving && Middle)
        {
            Finished = true;
            Middle = false;
            ManageScore.score += middlePoints;
            Ended = true;
        }

        if (!halfMoving && Center)
        {
            Finished = true;
            Center = false;
            ManageScore.score += centerPoints;
            Ended = true;
        }

        if (Ended == true)
        {
            SceneManager.LoadScene(sceneNumber, LoadSceneMode.Single);
        }
    }
}

Thanks for reading this everyone, I’d really appreciate some help with this. Thanks!

Are you sure sceneNumber has the value you want before you try to load the next scene? Maybe check with a Debug.Log before you try to load it.

 if (Ended == true)
             {
    Debug.Log("Next scene: " + sceneNumber);
    
                 SceneManager.LoadScene(sceneNumber, LoadSceneMode.Single);
             }

Did you add the scene in File → Build Settings?

You can add a delay, to prospone loading the scene until you did the things you wanted to do. Otherwise it will load instantly. You could use for example: Unity - Scripting API: MonoBehaviour.Invoke