Scene Changes and Loads

Hi, i’m new to Unity and im working on a menu for a 2D game, nothing crazy.

The problem i have i dont know if it’s related to the script im using or something im missing on Unity it self

I have a button in the menu to start playing and the script attached to it as On Click is this

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

public class MenuStart : MonoBehaviour
{
  public void Start()
   {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
   }
}

The thing is that when i run it, it just jumps to the next scene, without me pressing the “start” button

I have also tried an even more simplier code

using UnityEngine;
using UnityEngine.SceneManagement;

public class Control : MonoBehaviour
{
    public void NextScene()
    {
        SceneManager.LoadScene("1");
    }
}

But it has the same problem.

Im kinda lost with this, i dont know what could be the problem.

Thanks in advance.

The method name “Start” in Unity is reserved for a Unity event function that Unity automatically calls during the first frame the GameObject the script is on is active (and the script component is enabled), see here.
That explains why in the first case the LoadScene call is executed (basically) immediately when entering play mode.

The second code snippet you posted does not have that “problem”. Do you still have the first script in the scene?
If not and NextScene() it’s also directly executed then the method must be called by something you explicitly set up.

public class MenuStart : MonoBehaviour
{
  public void StartGame()
   {
        SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
   }
}

thank you so much, this actually helped a alot, i managed to make it work now :stuck_out_tongue: