- Home /
Question by
$$anonymous$$ · May 02, 2015 at 11:25 PM ·
saveload
Find SceneIndex out C#
Hello, im Writing a Save/Load script, and i would save the SceneIndex in the Registry using PlayerPrefs. Here The script:
using UnityEngine;
using System.Collections;
public class SaveLoad : MonoBehaviour {
public static int gold = 0;
public static int lifepoints = 0;
public bool loading;
public bool saving;
public InventoryManager invm;
public HealthC hc;
private SceneFader sceneFader;
void Awake()
{
sceneFader = GetComponent<SceneFader>();
}
void Update()
{
gold = (int) invm.coin;
lifepoints = (int)hc.lifePoints;
if (loading)
{
ActivateLoad();
}
if (saving)
{
Save();
}
}
private void ActivateLoad(){
if(PlayerPrefs.HasKey("SaveState"))
{
Debug.Log("Found SaveState");
Load();
Debug.Log("Gold:"+gold);
Debug.Log("Life:"+lifepoints);
}
else{
Debug.Log("Load: Save");
Save();
}
loading = false;
}
private void Save(){
saving = false;
PlayerPrefs.SetInt("SaveState", 1);
PlayerPrefs.SetInt("Gold", gold);
PlayerPrefs.SetInt("LifePoints", lifepoints);
}
private void Load(){
gold = PlayerPrefs.GetInt("Gold", 0);
lifepoints = PlayerPrefs.GetInt("LifePoints", 0);
InventoryManager.inventory.SetItems("coin",gold);
lifepoints = invm.initLifePoint;
sceneFader.SwitchScene (2); //Here I would to replace the 2 with the saved SceneIndex (SceneFader work like the Application.LoadLevel)
}
}
Thank's for Idears!
Comment
Answer by NoseKills · May 02, 2015 at 11:27 PM
Did you try saving and loading Application.LoadedLevel ? Or Application.LoadedLevelName in case your level order ever changes
Your answer
![](https://koobas.hobune.stream/wayback/20220613194514im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Saving/Load using menu C# 1 Answer
Loading a list into unity 0 Answers
PlayerPrefs 2 Answers
PlayerPrefs don't load again after Android game closes for the first time. 1 Answer
Saving and loading data from file 0 Answers