1
\$\begingroup\$

The script is attached to a button in the Inspector to the On Click: The script is attached to both StartGame and StartGameButton:

StartGameButton

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

public class LoadSceneOnClick : MonoBehaviour
{
    public void ActivatePlayer()
    {
        SceneManager.UnloadSceneAsync(0);
    }
}

The exception is on the line:

SceneManager.UnloadSceneAsync(0);

ArgumentException: Scene to unload is invalid
UnityEngine.SceneManagement.SceneManager.UnloadSceneAsync (Int32 sceneBuildIndex) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/SceneManagerBindings.gen.cs:196)
LoadSceneOnClick.ActivatePlayer () (at Assets/My Scripts/Scenes/LoadSceneOnClick.cs:10)
UnityEngine.Events.InvokableCall.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:165)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:36)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:45)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update()
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

You're getting the error because there's no scene with the build index 0. If you're certain a scene does exist, then you can either:

Pass its name instead: SceneManager.UnloadSceneAsync("MyScene");

You could also pass the scene as an object: SceneManager.UnloadSceneAsync(SceneManager.GetActiveScene());.

Or, if you really want to use the index, pass: SceneManager.UnloadSceneAsync(SceneManager.GetActiveScene().buildIndex);

UnloadScene() is obsolete and should not be used.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.