I have two scenes Scene00 and Battle00.
I made an empty object Data for Scene00 and a Button for Battle00.
I attached Data with C# scripts SaveSceneData.cs for Scene00 and BattleEnds.cs to the Button in Battle00. I just wanted to change the scenes between Scene00 and Battle00 by pressing a key Z and clicking the button but t I only get red underline (CS0103 C# The name does not exist in the current context) in SaveSceneData
in string lastScene = SaveSceneData.sceneName;
in visual studio and I get in unity
Cannot load scene: Invalid scene name (empty string) and invalid build index -1
UnityEngine.SceneManagement.SceneManager:LoadScene(String)
BattleEnds:OnClick() (at Assets/Scripts/Transition/BattleEnds.cs:21)
UnityEngine.EventSystems.EventSystem:Update()
when I try to play the game.
Do I need something special to refer to the public static string?
SaveSceneData.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.SceneManagement;
public class SaveSceneData : MonoBehaviour {
public static string sceneName;
void Start () {
sceneName = SceneManager.GetActiveScene().name;
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Z))
{
Application.LoadLevelAdditive("Battle00");
}
}
}
BattleEnds.cs
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
[RequireComponent(typeof(Button))]
public class BattleEnds : MonoBehaviour
{
private string lastScene;
void Start()
{
string lastScene = SaveSceneData.sceneName;
// Buttonクリック時、OnClickメソッドを呼び出す
GetComponent<Button>().onClick.AddListener(OnClick);
}
void OnClick()
{
// もとのシーンに遷移する
SceneManager.LoadScene(lastScene);
}
}