Unity - transferring data between scenes

How to transfer the rating value from one scene to another?

I tried the following:

Scene One:

void Start () {
    score = 0;
    updateScoreView ();
    StartCoroutine (DelayLoadlevel(20));
}

public void updateScoreView(){
    score_text.text = "The Score: "+ score;
}

public void AddNewScore(int NewscoreValue){
    score = score + NewscoreValue;
    updateScoreView ();
}

IEnumerator DelayLoadlevel(float seconds){        
    yield return new WaitForSeconds(10);
    secondsLeft = seconds;
    loadingStart = true;
    do {        
        yield return new WaitForSeconds(1);
    } while(--secondsLeft >0);

    // here I should store my last score before move to level two
    PlayerPrefs.SetInt ("player_score", score);
    Application.LoadLevel (2);
}

Scene Two:

public Text score_text;
private int old_score;

// Use this for initialization
void Start () {    
    old_score = PlayerPrefs.GetInt ("player_score");
    score_text.text = "new score" + old_score.ToString ();      
}

but nothing is displayed on the screen and there is no error.

Is it right to transfer data?

I use the free version of Unity 5, I’m developing a game for Gear VR (this means that the game will work on Android devices).

Any suggestion?

+4
source share
2 answers

Besides playerPrefs, another dirty way is to save the object during level loading by calling DontDestroyOnLoad on it.

DontDestroyOnLoad (transform.gameObject);

script, , , script . DontDestroyOnLoad GameObject, , , .

GameObject script , .

+5

3, , . / GameObjects static.

1. static.

, , MonoBehaviour GameObject, static.

, int, bool, string, float, double. static.

, :

static int counter = 0;
static bool enableAudio = 0;
static float timer = 100;

.


, :

public class MyTestScriptNoMonoBehaviour
{

}

static MyTestScriptNoMonoBehaviour testScriptNoMono;

void Start()
{
    testScriptNoMono = new MyTestScriptNoMonoBehaviour();
}

, MonoBehaviour. .


, :

, Object, Component GameObject .

1A. , MonoBehaviour

public class MyTestScript : MonoBehaviour 
{

}

static MyTestScript testScript;

void Start()
{
    testScript = gameObject.AddComponent<MyTestScript>();
} 

, MonoBehaviour.

1B. GameObject:

static GameObject obj;

void Start()
{
    obj = new GameObject("My Object");
}  

, GameObject GameObject Object.

Unity Object, static.

. # 2 .


2. DontDestroyOnLoad .

, Object, Component GameObject. , 1A 1B.

, GameObject :

void Awake() 
{
    DontDestroyOnLoad(transform.gameObject);
}

static 1A 1B:

public class MyTestScript : MonoBehaviour 
{

}

static MyTestScript testScript;

void Awake() 
{
    DontDestroyOnLoad(transform.gameObject);
}

void Start()
{
    testScript = gameObject.AddComponent<MyTestScript>();
} 

testScript .

3. , .

, , , . - , , , , ..

Thare - :

3A. PlayerPrefs API.

, . , :

int playerScore = 80;

playerScore:

OnDisable

void OnDisable()
{
    PlayerPrefs.SetInt("score", playerScore);
}

OnEnable

void OnEnable()
{
    playerScore  =  PlayerPrefs.GetInt("score");
}

3B. json, xml binaray, API #, File.WriteAllBytes File.ReadAllBytes .

, .

, , MonoBehaviour. , , -.

:

[Serializable]
public class PlayerInfo
{
    public List<int> ID = new List<int>();
    public List<int> Amounts = new List<int>();
    public int life = 0;
    public float highScore = 0;
}

DataSaver, File.WriteAllBytes File.ReadAllBytes, .

:

PlayerInfo saveData = new PlayerInfo();
saveData.life = 99;
saveData.highScore = 40;

PlayerInfo "":

DataSaver.saveData(saveData, "players");

"":

PlayerInfo loadedData = DataSaver.loadData<PlayerInfo>("players");
+7

All Articles