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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by tonyjoseph456 · Jul 22, 2015 at 05:40 PM · javascriptarraypersistentdatapathpersistentjavascript arrays

Error while trying to enter data to the array 2nd time.

 #pragma strict
 import System;
 import System.Runtime.Serialization.Formatters.Binary;
 import System.IO;
 
 var data:PlayerData;
 var LevelValue:int;
 var Level:int;
 var Coins:int;
 var UnlockedTopID:int;
 var UnlockedTops:Array;
 private var WorldScript:WorldNew;
 var LevelText:UnityEngine.UI.Text;
 var LevelMeter:UnityEngine.UI.Slider;
 var CoinCounter:UnityEngine.UI.Text;
 function Save()
 {
     var bf:BinaryFormatter=new BinaryFormatter();
     var file:FileStream=File.Create(Application.persistentDataPath +"/playerInfo.dat");
     data.LevelValue=LevelMeter.value;
     data.Level=WorldScript.Level;
     data.Coins=int.Parse(CoinCounter.text);
     bf.Serialize(file,data);
     file.Close();
 }
 
 function UnlockedTopSave(id:int)
 {
     ////This function is called when ever a purchase of an item in inventory is done with an id ranging from 1 to 10
     if(id!=0)
     {
         UnlockedTopID=id;
         UnlockedTops.Add(id);
         for(var j:int=0;j<UnlockedTops.length;j++)
         {
             data.UnlockedTops.Add(UnlockedTops[j]);
         }
         Debug.Log("Length of unlocked tops"+UnlockedTops.length);
         Save();
     }
 }
 
 function Load()
 {
     if(File.Exists(Application.persistentDataPath +"/playerInfo.dat"))
     {
         var bf:BinaryFormatter=new BinaryFormatter();
         var file:FileStream=File.Open(Application.persistentDataPath +"/playerInfo.dat",FileMode.Open);
         var data:PlayerData=bf.Deserialize(file) as PlayerData;
         LevelValue=data.LevelValue;
         Level=data.Level;
         Coins=data.Coins;
         UnlockedTops=data.UnlockedTops;
         file.Close();
     }
 }
 function Start () 
 {
 
     WorldScript=this.GetComponent(WorldNew);
     UnlockedTops=new Array();
     data=new PlayerData();
 }
 class PlayerData extends System.Object
 {
     var LevelValue:int;
     var Level:int;
     var Coins:int;
     var UnlockedTops:Array=new Array();
 }



This is my code of saving and loading data in my game. While I try to save a list of items using an array and their integer id, if I purchase the first item, it enters correctly to the array. THen if I quit the game play from the unity editor, and then plays again, the data is loaded correctly to the array, but when I again purchase a second thing , the 1st purchased data is overwritten. I need to save both the data on the second item purchase. Please help me to find the error in my code. Please do help me. Thanks in advance..

Comment
Add comment · Show 1
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 meat5000 ♦ · Jul 22, 2015 at 05:22 PM 0
Share

After you load the data, find the number of elements in your array, Array.length or List.count and use that figure to append to the end of your collection.

If your PlayerData is meant to emulate a struct you need to extend System.ValueType

You need to utilise a constructor for this to work properly.

Here's a script I use. Notice the function matches the class. You need to call this function when creating a new datablock.

 #pragma strict
 
 public class BlockStruct extends System.ValueType
 {
     var blockID : int;
     var blockType : int;
     var blockName : String;
     var blockPosition : int;
     var positionsWhenSpawned : int;
     var thisTransform : Transform;
     
     public function BlockStruct(     blockID : int,
                                     blockType : int,
                                     blockName : String,
                                     blockPosition : int,
                                     positionsWhenSpawned : int,
                                     thisTransform : Transform
                                 )
                                 {
                                     this.blockID = blockID;
                                     this.blockType = blockType;
                                     this.blockName = blockName;
                                     this.blockPosition = blockPosition;
                                     this.positionsWhenSpawned = positionsWhenSpawned;
                                     this.thisTransform = thisTransform;
                                 }
 }

To create a 'BlockStruct'

 private var paddingZero : BlockStruct = BlockStruct( 0, 0, "Padding", 0, 0, nullTransform); //This syntax look familiar? :)

So, make your PlayerData look like that and use the constructor function to create new data and you should find that your data is no longer defaulting, one would hope.

0 Replies

· Add your reply
  • Sort: 

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

Sorting arrays? 1 Answer

Ways to optimize this code? iterate through inventory 0 Answers

what is the difference between a float[] and a float[,]? 1 Answer

Parse Array data to generate html 0 Answers

Why ExecuteInEditMode Always Causes NullReferenceException Errors Even In Clamping!! 2 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