Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Lockstep · Jan 17, 2013 at 04:50 PM · nullreferenceexceptionplayerprefsloadxmldefault

"Simple State Saving" problem.

Hello. I've downloaded a small script for saving and loading data using xml serialization and playerprefs (Simple State Saving; sourcecode below). I wanted to try it out on a very simple test but I have encountered a problem when clearing the key. The LoadData method of the stateStorage class of the downloaded script should return the default value of my class. However the instance of my custom class becomes null instead and i have no idea why. Otherwise the script works fine.

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class Data {
     public Color _color = Color.red;
 }
 public class ColorSve : MonoBehaviour {
     public Data data = new Data();
     // some other variables
     
     void OnGUI() {
         
         redSlider = GUI.HorizontalSlider(redSliderArea, redSlider , 0f ,255f);
             //sliders for other colors
         UpdateColor();
         
         if(GUI.Button(loadButton, "LOAD")){
             data = StateStorage.LoadData<Data>("Data");
             if(data == null) Debug.Log("data is null");   //gets logged if I click the button after the clear button
             Debug.Log(data._color);
             UpdateSlider();
         }
         // two more buttons which call Statestorage.SaveData / ClearData
     }
     void UpdateSlider(){
         redSlider = data._color.r * 255f;
         //same for other sliders    
     }
     void UpdateColor(){
         Color temp = new Color (redSlider / 255f, greenSlider /255f, blueSlider /255f, 1f);
         data._color = temp;
         renderer.material.color = temp;
     }
 }
 

Source code of the Simple State Saving class. I only added debug lines and comments.

 using UnityEngine;
 using System.Xml.Serialization;
 using System.IO;
 
 public class StateStorage{
     
     public static T LoadData<T>( string key ){
         if ( PlayerPrefs.HasKey( key ) ) {        
             XmlSerializer serializer = new XmlSerializer( typeof( T ) );
             StringReader sr = new StringReader( PlayerPrefs.GetString( key ) );
             Debug.Log(PlayerPrefs.GetString(key));
             return ( T )serializer.Deserialize( sr );
         }else{
             Debug.Log("default");     //This gets correctly gets logged after I clear the key
             return default(T);
         }
     }    
     
     public static void SaveData<T>( string key, T source ){
         XmlSerializer serializer = new XmlSerializer( typeof( T ) );
         StringWriter sw = new StringWriter();
         serializer.Serialize( sw, source );
         PlayerPrefs.SetString( key, sw.ToString() );
         Debug.Log(sw.ToString());
     }
     
     public static void ClearData( string key ){
         PlayerPrefs.DeleteKey( key );
     }
     
 }
 


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
0
Best Answer

Answer by Lockstep · Jan 22, 2013 at 10:07 PM

My bad. I misunderstood the default key word. I thought it would return the default class i defined like new Data(); would have done. In reality default always returns 0 (for numeric value types), false (for booleans) or Null (else).

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

9 People are following this question.

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

Related Questions

Null Reference Exception even after check 1 Answer

Xml save/load script gives null reference exception in javascript 0 Answers

How to save resources configuration file for unity3d? 0 Answers

Using XML files and playerprefs together 0 Answers

What is the best way to save a deck of cards? 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