- Home /
1st Problem: OnLevelWasLoaded, method is getting called twice. 2nd: Loading PlayerPrefs
First Problem: As the title states, the method is getting called twice.
I have a scene, which have 2 doors. These doors have a script attached to them which has the OnLevelWasLoaded. When I enter this scene from another scene, the method gets called twice. My guess is that the script is being called from the 2 objects because they have the same script. (I've tested it with debug.log and it shows that the onlevelwasloaded was being called twice)
What I want to do is to make the method to get called only once from the door which I came into.
2nd Problem: I have trouble loading playerprefs. What I want to do is when I first enter the trigger (from scene1) which leads me to scene2, the playerprefs will won't load. And when I enter another trigger from scene2 which leads me to scene 1, I want the playerprefs to load.
In short, I want the playerprefs to won't load the first time i will enter a trigger to another scene, but will load on the future enters.
Here is the script I used, the onlevelwasloaded and playerprefs problem both exist in this script
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class Position : MonoBehaviour {
public Vector3 startPosition;
public Transform playerPosition;
public static Position Instance;
public float PlayerX;
public float PlayerY;
public float PlayerZ;
public float PlayerX2;
public float PlayerY2;
public float PlayerZ2;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnLevelWasLoaded(){
Debug.Log(PlayerPrefs.GetInt("Save"));
PlayerX2 = PlayerPrefs.GetFloat("PlayerX2");
PlayerY2 = PlayerPrefs.GetFloat("PlayerY2");
PlayerZ2 = PlayerPrefs.GetFloat("PlayerZ2");
PlayerX = (PlayerPrefs.GetFloat ("PlayerX"));
PlayerY = (PlayerPrefs.GetFloat ("PlayerY"));
PlayerZ = (PlayerPrefs.GetFloat ("PlayerZ"));
if (PlayerPrefs.GetInt("Save") == 1) {
LoadPos ();
PlayerPrefs.SetInt ("Save", 0);
PlayerPrefs.Save ();
}
PlayerPrefs.SetInt ("Save", 1);
PlayerPrefs.Save ();
}
public void LoadPos(){
startPosition = new Vector3 (PlayerX2, PlayerY2, PlayerZ2);
GameObject.FindWithTag("Player").transform.position = startPosition;
PlayerPrefs.DeleteAll ();
PlayerPrefs.Save ();
}
public void SavePos(){
PlayerPrefs.SetFloat("PlayerX2", PlayerX);
PlayerPrefs.SetFloat("PlayerY2", PlayerY);
PlayerPrefs.SetFloat("PlayerZ2", PlayerZ);
PlayerPrefs.SetFloat ("PlayerX", PosChar.Instance.PosX);
PlayerPrefs.SetFloat ("PlayerY", PosChar.Instance.PosY);
PlayerPrefs.SetFloat ("PlayerZ", PosChar.Instance.PosZ);
PlayerPrefs.Save ();
Debug.Log ("SAVED X:" + PlayerPrefs.GetFloat ("PlayerX") + " Y:" + PlayerPrefs.GetFloat ("PlayerY") + " Z:" + PlayerPrefs.GetFloat ("PlayerZ"));
Debug.Log ("Values for X2,Y2,Z2 X2:" + PlayerPrefs.GetFloat ("PlayerX2") + " Y2:" + PlayerPrefs.GetFloat ("PlayerY2") + " Z2:" + PlayerPrefs.GetFloat ("PlayerZ2"));
}
void OnTriggerEnter(Collider other){
if (other.tag == "Player") {
if (gameObject.name == "FromCityToLiving") {
SavePos ();
SceneManager.LoadScene ("living room");
Debug.Log (gameObject.name);
}
else if (gameObject.name == "FromLivingToBedroom") {
SavePos ();
SceneManager.LoadScene ("bedroom");
Debug.Log (gameObject.name);
}
else if (gameObject.name == "FromBedroomToLiving") {
SavePos ();
SceneManager.LoadScene ("living room");
Debug.Log (gameObject.name);
}
else if (gameObject.name == "FromLivingToCity") {
SavePos ();
SceneManager.LoadScene ("city");
Debug.Log (gameObject.name);
}
}
}
}
Answer by ricjonsu098 · Aug 05, 2016 at 01:02 PM
Since no one replied with an answer, I managed to figure out how to solve the two problem.
For the first problem, I created a singleton which holds the method OnLevelWasLoaded() and removed this method on the two scripts. So everytime I will change scene, instead putting the method on a door trigger, I putted it in the singleton so it will be called once from there.
Second problem, From the same singleton, I created a variable, which will increment every time OnLevelWasLoaded is called, and made a decision block which will only proceed if the level/scene was loaded twice and above.
Hope this will serve as a future reference for you guys.
Your answer
Follow this Question
Related Questions
Destroy and Object and PlayerPrefs 1 Answer
Unity Character Selection + store 1 Answer
How to stop objects you picked up from reappearing when you go to another level and return 1 Answer
How do I get my endless runner high score,How do I make high score for an endless runner 1 Answer
My playerprefs wont save and load my int to TMPro text 1 Answer