If I change a static variable in one scene will it remain changed when I load another scene?
for example if I use a script to change the variable
num from 1 to 2
then call on num in another scene will num be 1 or 2?
If I change a static variable in one scene will it remain changed when I load another scene?
for example if I use a script to change the variable
num from 1 to 2
then call on num in another scene will num be 1 or 2?
Static variables remember their values across scenes. So, num will be 2 in the next scene in your example.
Example:
using UnityEngine;
public class StaticExample : MonoBehaviour
{
public static int counter = 0;
void Start()
{
Debug.Log(counter++);
Invoke("ReloadLevel", 3);
}
void ReloadLevel()
{
Application.LoadLevel(Application.loadedLevel);
}
}
0
UnityEngine.Debug:Log(Object)
StaticExample:Start() (at Assets/StaticExample.cs:7)
1
UnityEngine.Debug:Log(Object)
StaticExample:Start() (at Assets/StaticExample.cs:7)
2
UnityEngine.Debug:Log(Object)
StaticExample:Start() (at Assets/StaticExample.cs:7)