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 ART_AGE · Aug 11, 2015 at 07:18 PM · arrayobjectsaveobjects

SAVE OBJECTS IN ARRAY

I made a script that should save and load an array of objects but when I load all objects are given the coordinates of one. What i do wrong ?

 using UnityEngine;
 using System;
 using System.Collections;
 using System.Collections.Generic;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 public class SaveObjects : MonoBehaviour {
 
     public Transform[] Object;
     public string FileName = "QuickSave";
 
     [System.Serializable]
     class PaR 
     {        
         public float OPX;
         public float OPY;
         public float OPZ;
     }
 
     public void Save(){
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = new FileStream(FileName, FileMode.Create);
         PaR newData = new PaR();
         for(int i = 0; i < Object.Length; i++)
         {
             newData.OPX = Object[i].transform.position.x;
             newData.OPY = Object[i].transform.position.y;
             newData.OPZ = Object[i].transform.position.z;
         }
         bf.Serialize(file, newData);
         file.Close();
     }
     public void Load(){
         if (File.Exists(FileName)){
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = new FileStream(FileName, FileMode.Open,FileAccess.Read);
             PaR newData1 = (PaR)bf.Deserialize(file);
             for(int i = 0; i < Object.Length; i++)        
             {
                 Object[i].transform.position = new Vector3(newData1.OPX,newData1.OPY,newData1.OPZ);
             }
             file.Close();
         }
     }
     void LateUpdate () {
         if (Input.GetKey (KeyCode.F5)) 
         {   
             Save();   
         } 
         if (Input.GetKey (KeyCode.F9)) 
         {   
             Load();   
         }
     }
 }
 

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

Answer by NeverHopeless · Aug 19, 2015 at 11:25 AM

You are overwriting the data in a single object, so it would just keep the last value.

  for(int i = 0; i < Object.Length; i++)
  {
      newData.OPX = Object[i].transform.position.x;
      newData.OPY = Object[i].transform.position.y;
      newData.OPZ = Object[i].transform.position.z;
  }

You should instead make an array of PaR. Try something like this:

  using System.Collections.Generic;

  public void Save(){
      BinaryFormatter bf = new BinaryFormatter();
      FileStream file = new FileStream(FileName, FileMode.Create);
      List<PaR> newDataCollection = new List<PaR>();
      for(int i = 0; i < Object.Length; i++)
      {
          PaR newData = new PaR();
          newData.OPX = Object[i].transform.position.x;
          newData.OPY = Object[i].transform.position.y;
          newData.OPZ = Object[i].transform.position.z;
          newDataCollection.Add(newData);
      }
      bf.Serialize(file, newDataCollection);
      file.Close();
  }

In the same way you can reload your data as well.

Comment
Add comment · Show 8 · 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 ART_AGE · Aug 30, 2015 at 08:14 PM 0
Share

It is not work objects loads with the coordinates of one

avatar image Cherno · Aug 31, 2015 at 08:30 AM 0
Share

^You have to re-write your load method as well, of course, so it accesses the newdata list.

avatar image ART_AGE · Aug 31, 2015 at 10:22 AM 0
Share

Can you sow me how it should look please ?

avatar image Mikilo · Aug 31, 2015 at 05:09 PM 1
Share

In Load()

          FileStream file = new FileStream (FileName, File$$anonymous$$ode.Create);

File$$anonymous$$ode.Create you should not create but Read.

avatar image ART_AGE · Aug 31, 2015 at 07:11 PM 1
Share

Thanks it works !

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

What are GameTiles? 1 Answer

Find objects by one tag or another. 3 Answers

boolean issue 1 Answer

Add custom properties to Objects 1 Answer

Convert Object[] to List 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