Quantcast
Channel: Questions in topic: "twice"
Viewing all articles
Browse latest Browse all 70

1st Problem: OnLevelWasLoaded, method is getting called twice. 2nd: Loading PlayerPrefs

$
0
0
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); } } } }

Viewing all articles
Browse latest Browse all 70

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>