Issue with saving checkpoint from portal with json

I implemented the json version of the saving scripts and saving using S and L is now working after some tinkering, but when I moved on to the portal checkpointing I get an error:

NullReferenceException: Object reference not set to an instance of an object
RPG.Movement.mover.RestoreFromJToken (Newtonsoft.Json.Linq.JToken state) (at Assets/Scripts/Movement/mover.cs:64)
RPG.Saving.JsonSaveableEntity.RestoreFromJToken (Newtonsoft.Json.Linq.JToken s) (at Assets/Scripts/Saving/JsonSaveableEntity.cs:47)
RPG.Saving.JsonSavingSystem.RestoreFromToken (Newtonsoft.Json.Linq.JObject state) (at Assets/Scripts/Saving/JsonSavingSystem.cs:124)
RPG.Saving.JsonSavingSystem.Load (System.String saveFile) (at Assets/Scripts/Saving/JsonSavingSystem.cs:53)
RPG.SceneManagement.SavingWrapper.Load () (at Assets/Scripts/SceneManagement/SavingWrapper.cs:35)
RPG.SceneManagement.Portal+d__9.MoveNext () (at Assets/Scripts/SceneManagement/Portal.cs:56)
UnityEngine.SetupCoroutine.InvokeMoveNext (System.Collections.IEnumerator enumerator, System.IntPtr returnValueAddress) (at <4746c126b0b54f3b834845974d1a9190>:0)

seems to be some issue in my RestoreFromJToken and points to the line disabling the navmeshagent, but I just copied it directly from the page.

 public void RestoreFromJToken(JToken state)
         {
             navMeshAgent.enabled = false;
             transform.position = state.ToVector3();
             navMeshAgent.enabled = true;
             GetComponent<ActionScheduler>().CancelCurrentAction();
         }
1 Like

At a guess, navMeshAgent is being cached in Start() in your Mover.cs script.
Any component reference that will be used inside of RestoreState() (or RestoreFromJToken) should be cached in Awake

void Awake()
{
    navMeshAgent = GetComponent<NavMeshAgent>();
}

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms