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 shepsaus000 · May 02, 2016 at 05:57 PM · scripting problemvariablescripting beginnersavepersistence

Complicated save problem

Alright, I have a terribly difficult problem for you guys out there (either that, or I'm a simple person :P):

So I have a saving system set up for my game that, so far, saves the player's exact position as well as a variable labeled "houseProgress" that essentially makes sure the game progress through its story:

Script that saves:

 using System.Collections;
 using System;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 
 public class GameControl : MonoBehaviour {
 
     public static GameControl control;
     public GameObject player;
 
     public Vector3 pos;
     public float houseProgress;
     public PlayerActivate playerActivateScript;
 
     void Awake(){
 
         player = GameObject.FindGameObjectWithTag ("Player");
         playerActivateScript = player.GetComponent<PlayerActivate> ();
         if (control == null) {
             DontDestroyOnLoad (gameObject);
             control = this;
         } else if (control != this) {
             Destroy (gameObject);
         }
 
     }
 
     void Update(){
         if (player == null) {
             player = GameObject.FindGameObjectWithTag ("Player");
             playerActivateScript = player.GetComponent<PlayerActivate> ();
         }
     }
 
     public void Save()
     {
         //playerPosition
         PlayerPrefs.SetFloat ("x", player.transform.position.x);
         PlayerPrefs.SetFloat ("y", player.transform.position.y);
         PlayerPrefs.SetFloat ("z", player.transform.position.z);
         PlayerPrefs.SetFloat ("houseProgress", playerActivateScript.houseProgress);
     }
 
     public void Load()
     {
 
             player.transform.position = new Vector3 (PlayerPrefs.GetFloat ("x"), PlayerPrefs.GetFloat ("y"), PlayerPrefs.GetFloat ("z"));
 
     }
 }


This script does alright when it comes to saving the player's position. The only problem is that, when the scene changes (which always happens alongside the save script), I get a TON of null reference errors in some scenes, and for good reason because the player object does not exist in some scenes I have set up, so hopefully someone can help me deal with this problem.

So that's the first problem, the second regarding the save script is the houseProgress variable. It is a float that increases by one every time the player does something. However, it doesn't seem to want to save what it is at. Here is the script I have set up so far that affects it:

 if (check.isColliding == true) {
                             houseProgress += 1;
                             GameControl.control.Save ();
                             SceneManager.LoadScene ("Journal Entry 1");
                         }


I think I know the problem may lie with the variable is tied to the player, which isn't in the Journal Entry scene, so it resets back to the value of 0, so I want to know how I can make this persist.

And that is it! If anyone needs ANY information, please let me know!

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

2 Replies

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

Answer by TBruce · May 02, 2016 at 06:25 PM

@shepsaus000

Start with this, it should at least correct all your errors

 using System.Collections;
 using System;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 public class GameControl : MonoBehaviour
 {
     public static GameControl control;
     public GameObject player;
     public Vector3 pos;
     public float houseProgress;
     public PlayerActivate playerActivateScript;
 
     void Awake()
     {
         UpdatePlaerInfo();
         if (control == null)
         {
             DontDestroyOnLoad (gameObject);
             control = this;
         } else if (control != this)
         {
             Destroy (gameObject);
         }
     }
 
     void Update()
     {
         UpdatePlaerInfo();
     }
 
     void UpdatePlaerInfo()
     {
         if (player == null)
         {
             player = GameObject.FindGameObjectWithTag ("Player");
         }
         if ((player != null) && (playerActivateScript == null)))
         {
             playerActivateScript = player.GetComponent<PlayerActivate> ();
         }
     }
 
     public void Save()
     {
         //playerPosition
         if (player != null)
         {
             PlayerPrefs.SetFloat ("x", player.transform.position.x);
             PlayerPrefs.SetFloat ("y", player.transform.position.y);
             PlayerPrefs.SetFloat ("z", player.transform.position.z);
         }
         if (playerActivateScript != null)
         {
             PlayerPrefs.SetFloat ("houseProgress", playerActivateScript.houseProgress);
         }
     }
 
     public void Load()
     {
         if ((player != null) &&
             (PlayerPrefs.GetKey("x") != null) &&
             (PlayerPrefs.GetKey("y") != null) &&
             (PlayerPrefs.GetKey("z") != null))
         {
             player.transform.position = new Vector3 (PlayerPrefs.GetFloat ("x"), PlayerPrefs.GetFloat ("y"), PlayerPrefs.GetFloat ("z"));
         }
     }
 }
Comment
Add comment · Show 4 · 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
avatar image Outliver · May 02, 2016 at 06:37 PM 0
Share

yeah, but it won't probably save anything for as long as everything is null at call time.

avatar image shepsaus000 · May 03, 2016 at 04:21 PM 0
Share

This helped out a ton man! Thanks!

avatar image TBruce shepsaus000 · May 03, 2016 at 04:32 PM 0
Share

@shepsaus000

I am happy that you could get it working.

Edit: I would also like to add that calling UpdatePlaerInfo() in the Update function could substantially have an effect on performance. If you have not already considered this you may want to do this at intervals.

avatar image shepsaus000 TBruce · May 04, 2016 at 03:00 PM 0
Share

Yeah, I'm very much aware of that. Strangely though, it doesn't seem to be having a ton of performance problems when play testing. Nonetheless, I'll make sure I take care of that.

avatar image
1

Answer by Outliver · May 02, 2016 at 06:27 PM

I don't quite get you point. You're already giving the answers in your own very question.

First, make sure, you save the settings BEFORE loading a new scene.

Second, objects are always treated as references. Therefore, when you load your new scene without the player object, your playerActiveScript gets nulled. You would therefore need to do another GetComponent() call.

Comment
Add comment · Show 2 · 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
avatar image shepsaus000 · May 03, 2016 at 04:20 PM 0
Share

After reading this, I decided to go back and figure out if I overlooked anything, because whenever I get replies like these, I know for a FACT that I did.

Sure enough, I did.

You see, my first problem was pretty easy to fix, and I wish I could accept two answers because the other guy did indeed help me get rid of all of those errors showing up when I was in a scene without a player object.

As for my second problem, I found out that I needed to use that PlayerUpdateInfo function in the load function (which is where the problem was actually taking place) so that, when a load is called, the player object could be found while the load was executing, which it wasn't earlier, thus the houseProgress variable wasn't loading. :/

avatar image Outliver shepsaus000 · May 03, 2016 at 06:44 PM 0
Share

True heroes never get the pay-off ;) Glad, I could help and that you got your problem solved :)

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

50 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Player Prefs Dosen't work on android 0 Answers

How to subtract from a variable using a variable from another script 1 Answer

Counting prefabs in screen?,Problem with counting prefabs in screen 1 Answer

Limit player speed (2D) 3 Answers

Pong goal collision 3 Answers


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