Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 DEJVOSS · Jan 07 at 06:52 PM · jsonforeachsaveload

Saving gameobjects data to JSON.

Hi there, I am trying to loop through all the gameobjects with a tag and save their data like position, rotation etc.. to a JSON file, however the JSON only contains data for the last object it loops through. What's weird is when I Debug.Log(json.ToString()); it returns the JSON data just fine.

     SaveLoadObjects data = new SaveLoadObjects();
     Debug.Log("Saving file as level" + levelInt + "...");

     GameObject[] objects = GameObject.FindGameObjectsWithTag("Object");

     foreach (GameObject obj in objects)
     {
         data.objName = obj.name;
         data.posX = obj.transform.position.x;
         data.posY = obj.transform.position.y;
         data.posZ = obj.transform.position.z;

         string json = JsonUtility.ToJson(data, true);

         Debug.Log(json.ToString());
         File.WriteAllText("level" + levelInt + ".phbl", json);
     }
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 DEJVOSS · Jan 07 at 06:55 PM 0
Share

Forgot to include images.

alt text

alt text

lvlsave-console.png (12.8 kB)
lvlsave-json.png (7.0 kB)

1 Reply

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

Answer by Llama_w_2Ls · Jan 07 at 07:03 PM

You are using the IO function File.WriteAllText instead of File.AppendText. WriteAll deletes all content in that file then writes to it (overwrites it). This will mean only your last object remains. Instead use file.append, or use a stringbuilder and append to the stringbuilder, then write all your text at once. For example:

      SaveLoadObjects data = new SaveLoadObjects();
      Debug.Log("Saving file as level" + levelInt + "...");
      GameObject[] objects = GameObject.FindGameObjectsWithTag("Object");
      var sb = new StringBuilder();
 
      foreach (GameObject obj in objects)
      {
          data.objName = obj.name;
          data.posX = obj.transform.position.x;
          data.posY = obj.transform.position.y;
          data.posZ = obj.transform.position.z;
          string json = JsonUtility.ToJson(data, true);
          Debug.Log(json.ToString());
          sb.AppendLine(json);
      }
 
 File.WriteAllText("level" + levelInt + ".phbl", sb.ToString());


@DEJVOSS

Comment
Add comment · Show 1 · 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 DEJVOSS · Jan 07 at 07:08 PM 0
Share

Thanks mate!

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

135 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 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 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

How to get values from nested JSON data with foreach or for loop? 1 Answer

Saving in json an object inside a save class 0 Answers

How can I figure if an Object was created at runtime? (for extending Json save/load) 0 Answers

Save Game format that is change proof and secure 0 Answers

save and load json score to web/server 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