Invalid Scene Name Error (empty string) but everything still works as intended

I’m having a peculiar issue where i’m trying to load a scene by name when interacting with a doorway and I keep getting the “Invalid scene name (empty string) and invalid build index -1” error. The thing is though is that it is still loading the specified scene properly and everything works no problem. For example using the code below:

public string sceneNameToLoad; (for this example the name is defined as "Scene2" in the inspector)

void Update()
{
    if (detectsPlayer && Input.GetButtonUp("Interact")) {
        SceneManager.LoadScene(sceneNameToLoad); //This is the line getting the error
    }
}

Upon interacting with the object the error shows up, but Scene2 is still loaded like normal. If the name that is being loaded is changed, it still works completely fine and as expected and the game keeps running but still throws out the error. How do I get this error to go away and why is it even showing up?

full script:

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


public class SceneTransition : MonoBehaviour
{
    [Tooltip("Set to require input to transfer scenes. False automatically changes scenes when the player gets close enough")]
    public bool interactable;
    public string sceneNameToLoad ;
    public float fadeTime = 1.0f;
    private bool detectsPlayer = false;


    // Update is called once per frame
    void Update()
    {
        if (detectsPlayer && Input.GetButtonUp("Interact")) {
            GameObject.Find("Save Manager").GetComponent<SaveManager>().SaveGame();
            SceneManager.LoadScene(sceneNameToLoad);
        }
    }

    private IEnumerator FadeIn(SpriteRenderer interactSprite) {
        Color temp = interactSprite.color;
        while (temp.a < 1.0f) {
            temp.a += Time.deltaTime / fadeTime;
            if (temp.a >= 1.0f)
                temp.a = 1.0f;
            interactSprite.color = temp;
            yield return null;
        }
    }
    private IEnumerator FadeOut(SpriteRenderer interactSprite)
    {
        Color temp = interactSprite.color;
        while (temp.a > 0.0f)
        {
            temp.a -= Time.deltaTime / fadeTime;
            if (temp.a <= 0.0f)
                temp.a = 0.0f;
            interactSprite.color = temp;
            yield return null;
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == "Player") { 
            detectsPlayer = true;
            if (interactable) {   
                StartCoroutine(FadeIn(GetComponent<SpriteRenderer>()));
            } else if (!interactable) {
                GameObject.Find("Save Manager").GetComponent<SaveManager>().SaveGame();
                SceneManager.LoadScene(sceneNameToLoad);
            }
        }
    }
    private void OnTriggerExit(Collider other)
    {
        if (other.tag == "Player") { 
            detectsPlayer = false;
            if (interactable)
            {
                StartCoroutine(FadeOut(GetComponent<SpriteRenderer>()));
            }
        }
    }
}

1 Answer

1

That error message appears only when the scene can’t be loaded.
If your scene gets loaded there is a chance you have multiple SceneTransition scripts in your scene.
If you search in the hierarchy window for SceneTransition you should be able to find all instances.