Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ricjonsu098 · Aug 03, 2016 at 06:36 AM · scripting problemplayerprefsonlevelwasloadedtwice

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);
 
             }
         }
 
     }
 }    
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges