- Home /
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);
}
}
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.
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
the problem is in Serializer.cs ... line 37 .. what is that line?
This one, sir: string json = JsonUtility.ToJson(s1.output);
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?
Your answer
Follow this Question
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