Support Forum

Graph gets unloaded when Items in scene are changed

Hi,

So I believe I have set up my grid graph correctly but whenever I make changes to my current scene the graph will get unloaded and I will get a “No graphs in scene error”.

I’ve tried to address this issue by loading the graph from a file like so

public class GenerateNavMesh : MonoBehaviour
{
    AstarPath path;

    private void Awake()
    {
        path = this.GetComponent<AstarPath>();
        byte[] bytes = path.data.SerializeGraphs();

        AstarPath.active.data.DeserializeGraphs(bytes);
        AstarPath.active.Scan();
    }
}

But this seems to be ineffective.

Here’s my settings on the pathfinding component.

If I rescan the graph again and hit the play button, it will work again, but after i hit the play button a few more times or change something in my scene, it will break again.

Hi

Normally the graph is always loaded and scanned during Awake (assuming A* Inspector -> Settings -> scan on awake is enabled). You should not need to run that code during awake.

When exactly do you get this? Do not try to access graphs during an Awake call as the graph might not be ready then (the AstarPath component’s Awake method might not yet have been called).

It doesn’t seem to happen very consistently for me, but whenever I make a change in my scene (unrelated to the pathfinding component), I will have to rescan and save my scene which doesn’t seem to be a problem for other people.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using Pathfinding;

public class GenerateNavMesh : MonoBehaviour
{
    public TextAsset graph;

    private void Start()
    {
        AstarPath.active.data.DeserializeGraphs(graph.bytes);
        AstarPath.active.Scan();
        AstarPath.active.logPathResults = PathLog.None;

    }
}

I’ve changed to do this, which works fine when playing and testing the current scene I’m working on. But if I try to load the scene from a title menu or the scene itself gets reloaded (like player death), AIPathing seems to break. They have very small paths that they seem to not be able to complete.
29%20AM

Also I’m using the AIPath component for pathfinding.

Also here are my settings

Note that whenever scripts are recompiled the nodes of the graph will be lost and you will have to scan the graph again. This is because that data is too much to handle for Unity’s Undo/Serialization system.
Maybe this is what you are seeing?

1 Like

I have the same issue whenever I change code and go back to play the scene. Is there a way around this in the script so I don’t have to select the AStar game object each play or scan again?

@LunarMountain The package does not support hot-reloading. However as long as A* Inspector -> Settings -> Scan On Awake is enabled it will scan the graph when the game starts. You can also use a shortcut ctrl+alt+s to scan the graph.

Okay, I have it scanning on awake but what happens between debugs is the graph doesn’t generate. If I create it in the script, I get a null reference exception on graphTypes, and if I create a graph in the inspector, it disappears between plays.

What do you mean “between debugs”?

Hitting play button, stopping play, hitting play again. If I don’t have the A* game object selected that is.

Happens in builds as well. Not sure how to remedy.