Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 elrana112 · Jan 26, 2020 at 07:56 PM · vector3listjsonienumeratorfiles

Saving list of vector 3 using JSON

Hello, all! I am a newbie and I am trying to save a list of vector 3 from another script, but I am getting this run-time error: NullReferenceException: Object reference not set to an instance of an object Serializer+d__3.MoveNext () (at Assets/Serializer.cs:16) UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using System.IO;
 using Mapbox.Unity.MeshGeneration.Factories;
 public class Serializer : MonoBehaviour
 {
     static readonly string SAVE_FILE = "player.json";
 
     DirectionsFactory s1;
     
     IEnumerator Start()
     {
          s1 = gameObject.GetComponent<DirectionsFactory>();
          yield return new WaitForEndOfFrame();
         string json = JsonUtility.ToJson(s1.output);
         string filename = Path.Combine(Application.persistentDataPath, SAVE_FILE);
         if(File.Exists(filename))
         {
             File.Delete(filename);
         }
         File.WriteAllText(filename,json);
         Debug.Log("Player saved at "+ filename);
 }
 }
 
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 sacredgeometry · Jan 26, 2020 at 07:58 PM

I would suggest using


https://www.newtonsoft.com/json

with:

 var json = JsonConvert.SerializeObject(myVector3s); // To Serialise
 var myVector3s = JsonConvert.DeserializeObject<List<Vector3>>(json); // To Deserialise



In conjunction with FileStreams to save and load the json file.

https://docs.microsoft.com/en-us/dotnet/api/system.io.filestream?view=netframework-4.8


That said your bug is a simple nullreferenceexception.

It tells you the line its on in the error and all you need to do is find out which object you are trying to use is null and make sure it isnt before you try to use it.

If you cant see try attaching the debugger and putting a breakpoint on the line then by making sure everything is available.

Comment
Add comment · Show 9 · 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 elrana112 · Jan 26, 2020 at 08:53 PM 0
Share

Hello, @sacredgeometry ! Thanks for the fast reply! I did some research before the asking here about the 'NullReferenceException'. I even tried to put a simple if condition to check if the list is empty or not, but without any useful result. $$anonymous$$y list is driven by this piece of code:

 var dat = new List<Vector3>();
 foreach (var point in response.Routes[0].Geometry)
 {
     dat.Add(Conversions.GeoToWorldPosition(point.x, point.y, _map.Center$$anonymous$$ercator, _map.WorldRelativeScale).ToVector3xz());
 }

If you want to see the whole code, here it is (search for void HandleDirectionsResponse(DirectionsResponse response) and var dat is output variable I am trying to save) :link text

avatar image sacredgeometry elrana112 · Jan 26, 2020 at 09:04 PM 0
Share

the problem is in Serializer.cs ... line 37 .. what is that line?

avatar image elrana112 sacredgeometry · Jan 26, 2020 at 09:09 PM 0
Share

This one, sir: string json = JsonUtility.ToJson(s1.output);

Show more comments
avatar image Giacogiak · Oct 13, 2020 at 10:01 AM 0
Share

Hello ,

I tried to serialize a list of Vector3 just as in your example, but I get this error:

 JsonSerializationException: Self referencing loop detected for property 'normalized' with type 'UnityEngine.Vector3'. Path '[0].normalized'.

I do you prevent from having the same?

avatar image krishnanpc · Apr 01, 2021 at 12:45 PM 0
Share

@Giacogiak : https://makolyte.com/newtonsoft-json-jsonserializationexception-self-referencing-loop-detected-for-property/

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

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

A node in a childnode? 1 Answer

Monodevelop Does not Access List or IEnumerator? 3 Answers

How to animate a list of GameObject from their localposition 1 Answer

Object Not Moving - Yield - IEnumerator 1 Answer

How do you interate through a JSON object with a foreach loop to add each element to a list? 0 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