how to use public static variables?

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);
    }
}

You have a potential race condition here. In Start of your first script (SaveSceneData) you initialize the static variable with the name of the current scene. However at the same time in Start of the second script (BattleEnds) you read the same static variable and copy it’s content into a member variable of that script.

It’s possible that Start of the “BattleEnds” script runs before the Start of your SaveSceneData script. In that case the static variable is not initialized yet. However you have another issue. You declare a second local variable “lastScene” inside your Start method. That means you never actually initialize the private member variable lastScene. You would want to do something like this:

private string lastScene;

void Start()
{
    lastScene = SaveSceneData.sceneName;
    // ...

However why do you actually copy the content into a seperate variable? Just use the static variable when you load the scene. This eliminates the race condition problem and makes that extra variable useless.

void OnClick()
{
    SceneManager.LoadScene(SaveSceneData.sceneName);
}

If, for what reason ever, you want to keep that private member variable (lastScene) you have to make sure sceneName is initialized before you access it from the other script. So either use Awake instead of Start in the “SaveSceneData” class, or setup an explicit script execution order to make sure the order of execution is correct.

Thank you very much, the problem’s solved and I realised there’s something I did not know about!