Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Nikkimouse · Aug 14, 2015 at 10:23 AM · scripting problem

Is there a reason why Unity would be able to read one variable in a script and not another?

I have a single script attached to a gamemanager object in my game that has two sections: one as a GameManager that holds data between scenes and one as a Serializable section which converts the data from the GameManager to enable the Save function to save it.

If I play the game through from scene 1 - scene 3 and then save, the variables are all successfully read by my Save function (in a separate script). If, however, I load a previous game, I run into problems. The data is successfully loaded into the game and I can play it (ie. the GameManager.control.xxxx variables successfully use the loaded data), but when I go to save again, I can't because the game cannot find the Serializable section (PlayerData.current.xxxx). ie. Unity says object reference is not set to an object for the following line of the Save function:

    Debug.Log ("Saved Data: " + PlayerData.current.healthpotion);

This is despite the fact it's already accessed it to make the original save file. Is there any reason why a game wouldn't be able to see another section of the same script?

Main script

 using UnityEngine;
 using System.Collections;
 
 public class GameManager : MonoBehaviour {
 
     public static GameManager control;
 
     //recording names
     public string username;
 
     //setting up potion score
     public int hlthpotion;
     public int mnpotion;
 
 ....
 
 [System.Serializable]
 public class PlayerData {
     public static PlayerData current;
     public string UserName;
     public int healthpotion;
     public int manapotion;
     public bool redWizard;
     public bool purpleWizard;
     public bool blueWizard;
 
 
     public PlayerData () {
         UserName = GameManager.control.username;
         redWizard = GameManager.control.redwizard;
         blueWizard = GameManager.control.bluewizard;
         purpleWizard = GameManager.control.purplewizard;
         healthpotion = GameManager.control.hlthpotion;
         manapotion = GameManager.control.mnpotion;
 
     }
 }

Loading/Saving script

     using UnityEngine;
     using System.Collections;
     using System.Collections.Generic;
     using System.Runtime.Serialization.Formatters.Binary;
     using System.IO;
     
     public static class SaveLoad {
     
         public static List<PlayerData> savedGames = new List<PlayerData> ();
     
         public static void Save () {
             Debug.Log ("Original Data: " + GameManager.control.hlthpotion);
             Debug.Log ("Saved Data: " + PlayerData.current.healthpotion);
             PlayerData.current.healthpotion = GameManager.control.hlthpotion;
             PlayerData.current.manapotion = GameManager.control.mnpotion;
             Debug.Log ("Game To Be Saved");
             Debug.Log ("Original Data" + GameManager.control.hlthpotion);
             Debug.Log ("Saved Data" + PlayerData.current.healthpotion);
             SaveLoad.savedGames.Add (PlayerData.current);
             Debug.Log ("Game Added");
             BinaryFormatter bf = new BinaryFormatter ();
             FileStream file = File.Create (Application.persistentDataPath + "/playerInfo.dat");
             bf.Serialize (file, SaveLoad.savedGames);
             file.Close ();
             Debug.Log ("Game Saved " + PlayerData.current.UserName);
         }
 
      public static void Load() {
         if (File.Exists (Application.persistentDataPath + "/playerInfo.dat")) {
             Debug.Log ("Save File exists");
             BinaryFormatter bf = new BinaryFormatter ();
             FileStream file = File.Open (Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
 //            SaveLoad.savedGames = (List<PlayerData>)bf.Deserialize (file);
             List<PlayerData> playerDataList = (List<PlayerData>)bf.Deserialize (file);
             file.Close ();
             Debug.Log ("Save File Closed");
             foreach (PlayerData Pd in playerDataList) {    
                 GameManager.control.username = Pd.UserName;
                 Debug.Log ("Username matched");
                 GameManager.control.redwizard = Pd.redWizard;
                 Debug.Log ("Red matched");
                 GameManager.control.bluewizard = Pd.blueWizard;
                 Debug.Log ("Blue matched");
                 GameManager.control.purplewizard = Pd.purpleWizard;
                 Debug.Log ("Purple matched");
                 GameManager.control.hlthpotion = Pd.healthpotion;
                 Debug.Log ("Health Potion = " + GameManager.control.hlthpotion);
                 GameManager.control.mnpotion = Pd.manapotion;
     //            Debug.Log ("Mana Potion: " + PlayerData.current.manapotion);
             }
 
         }
     }
 }








Comment
Add comment · Show 7
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 Scribe · Aug 14, 2015 at 11:15 AM 0
Share

So where is the Load function you call?

avatar image Nikkimouse · Aug 14, 2015 at 12:17 PM 0
Share

Whoops. Sorry. Added that.

avatar image Scribe · Aug 14, 2015 at 12:23 PM 0
Share

You don't set PlayerData.current anywhere so it will be null, therefore PlayerData.current.xxxxx doesn't exist.

avatar image Nikkimouse · Aug 14, 2015 at 12:39 PM 0
Share

I thought that might be it and tried setting it, but couldn't work out what I could set it to that would work. I tried setting PlayerData.current = Pd within the foreach loop, and that got me an "Object Reference not set to object" error. What else could I try setting it to.

avatar image Scribe · Aug 14, 2015 at 12:45 PM 0
Share

Surely your current one is whichever you are choosing to load? I'm not sure why you are loading all your Pd's anyway, Game$$anonymous$$anager.control will just be overriden each time and you will only ever be able to load the most recently saved data.

Can you add Debug.Log(Pd) in your foreach and see if it's null?

Show more comments

1 Reply

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

Answer by FortisVenaliter · Aug 20, 2015 at 10:17 PM

It's because static fields are not serialized. What you can do is instead of having that as a static field in the PlayerData class, put it as a non-static field in the GameManager (which I assume is a singleton?). Then, if you want to keep references, you can just modify the old current variable as such:

 public static PlayerData current { get { return GameManager.singleton.playerData; } }

Or something like that. Then all references would work, and it should serialize properly.

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

How to switch between scripts on trigger? 1 Answer

Instantiate Objects from text file 1 Answer

I am trying to spawn and move a asteroid but the spawnner script doesnot access the script attached to the asteroid. 1 Answer

Orbit Camera on Survival Shooter Sample Project 0 Answers

Color Game Help: Movement, Score, and Game Over Script Problem 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