- Home /
Unity 5.5.1f1 Save and load data
I'm having an issue I've tried all sorts of soloutions using PlayerPref.SetInt() and PlayerPrefs.GetString()
I have game I'm working on two levels so far but I need info saved as the player passes through scenes. He collects a battery on scene 2 and then has to eventually go back to scene 1 to operate consoles after putting in batteries on scene2 and getting power to all consoles.
I am having difficulty having that data saved when going back to scene 1 any way I can get help?
This is the gamemanager script.
////////////////////////////////////////////////////////
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using UnityEngine.SceneManagement;
public class LevelManager : MonoBehaviour {
//controllerfor player
public CharacterMover characterMover;
//PowerUp Variables
public AudioSource powerUp;
public float hideDelay;
// string for level, levels are pixelworld , and pixelworld2
public string leveltoload;
public Text batteryText;
public int batteryCollected;
public int batteryacquired = 0;
//Console Variables
public Canvas console1Canvas;
public Canvas console2Canvas;
public Canvas console3Canvas;
public Canvas console4Canvas;
public Canvas tableCanvas;
public AudioSource computerError;
//
//ConsoleGameObjects
public GameObject console1;
public GameObject console2;
public GameObject console3;
public GameObject table;
//Game Doors
public GameObject door1;
public GameObject door2;
// Use this for initialization
public void Start ()
{
characterMover = FindObjectOfType<CharacterMover> ();
PlayerPrefs.GetInt ("batteryacquired", 1);
PlayerPrefs.GetInt ("batteryCollected");
PlayerPrefs.GetString ("batteryText", "batteryText");
}
public void Awake()
{
saveData ();
}
public void saveData()
{
if (PlayerPrefs.HasKey ("batteryText"))
{
PlayerPrefs.GetInt ("batteryacquired", 1);
PlayerPrefs.GetInt ("batteryCollected",1);
PlayerPrefs.GetString ("batteryText", "batteryText");
Debug.Log ("This info is running");
}
}
public void LoadData()
{
if (PlayerPrefs.HasKey ("batteryText"))
{
PlayerPrefs.SetInt ("batteryacquired", 1);
PlayerPrefs.SetInt ("batteryCollected",1);
PlayerPrefs.SetString ("batteryText", "batteryText");
Debug.Log ("This info is running");
}
}
}
/////////////////////////////////////////////////////////////////
power pick up script
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class PowerUp : MonoBehaviour {
public LevelManager levelManagerScript;
public AudioSource powerUp;
public float hideDelay;
public Text batteryText;
public int batteryCollected;
void start()
{
levelManagerScript = FindObjectOfType<LevelManager> ();
}
public void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
powerUp.Play ();
batteryCollected++;
batteryText.text = "Collected: " + batteryCollected;
if(levelManagerScript != null)
{
levelManagerScript.batteryacquired = 1;
PlayerPrefs.SetInt ("batteryacquired", 1);
PlayerPrefs.SetInt ("batteryCollected",1);
PlayerPrefs.SetString ("batteryText", "batteryText");
PlayerPrefs.Save ();
}
StartCoroutine (HideDelay ());
}
}
public IEnumerator HideDelay()
{
yield return new WaitForSeconds (hideDelay);
gameObject.SetActive (false);
}
} ///////////////////////////////////////////////////////////////////////////////////////////////////
load scene 1
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class LoadLevel : MonoBehaviour {
public LevelManager levelManagerScript;
// Use this for initialization
void Start ()
{
levelManagerScript = FindObjectOfType<LevelManager> ();
levelManagerScript.LoadData ();
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
SceneManager.LoadScene (levelManagerScript.leveltoload);
levelManagerScript.saveData ();
}
}
} ////////////////////////////////////////////////////////////////////////////////////////////
load scene 2
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement;
public class LoadLevel_ : MonoBehaviour {
public LevelManager levelManagerScript;
// Use this for initialization
void Start ()
{
levelManagerScript = FindObjectOfType<LevelManager> ();
levelManagerScript.saveData ();
}
//batteryText
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
SceneManager.LoadScene (levelManagerScript.leveltoload);
levelManagerScript.LoadData ();
}
}
} //////////////////////////////////////////////////////////////////////////////////////////////////////
Your load method doesn't load, it saves. Try something like
playerBattery = PlayerPrefs.GetInt("batteryacquired");
Your answer
Follow this Question
Related Questions
Change Scene keeping Character and GUI Canvas 1 Answer
Scene loading problem 5 Answers
Help with error message "Overwriting the same path as another open scene is not allowed" 0 Answers
CheckConsistency: GameObject does not reference component Animator. Fixing. 0 Answers
Scene loads in editor not in build 0 Answers