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);
}
}

I have corrected the json.net for vectors. There are instructions on how to install and use.
That solves all the problems! :slight_smile:
GitHub - slavaWins/JsonUnityVectors: Fix json net for unity Vectors

I would suggest using


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.


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.