Static Variable Saving Issue

So I have a static variable named “Music” which is all well and good, but I’m trying to load its value across two scenes. However, in the first scene I set it to “true”, and in the second scene its loading at “false”. Why? How do I fix this?

First Script:

    using UnityEngine;
    using System.Collections;
    
    public class ChangeMusic : MonoBehaviour {
    	static public bool Music;
    	public void MusicChange() {
    		Debug.Log("Music val has changed to " + Music);
    		Music = !Music;
    		//Component otherscript = GameObject.FindGameObjectWithTag("PlayerPrefs").GetComponent("Music Script");
    	}
    }

Second Script:

using UnityEngine;
using System.Collections;

public class newMusicScript : MonoBehaviour {

	void Awake() {
		Debug.Log("Msusic is logged at " + ChangeMusic.Music);
		bool music = ChangeMusic.Music;
		Debug.Log("Music is logged at " + music);
		if(music != false){
			GameObject.FindGameObjectWithTag("BackgroundMusic").audio.Play();
		}
		if(music == false){
			GameObject.FindGameObjectWithTag("BackgroundMusic").audio.Stop();
		}
	}
}

Static means it exists on the class(in this case, as a field of all instances sharing the same value space) not the instance(where the non-static property/field would be unique(maybe) between objects). When switching scenes and because you didn’t mention it, your objects are destroyed and rebuild if associated with the new scene. A boolean value has a default value of false if not initialized otherwise during declaration.

If you would like to persist an object between scene loads, please use DontDestroyOnLoad

Hi,
In the first script you have not called the method MusicChange().
Call it in Start of the first script.
i hope it helps.