"The variable xy of XYZ doesn't exist anymore"

Verdemis

New member
[EDIT] I posted this in the wrong forum by mistake at first. So I reposted it here. Sorry.[/EDIT]

Hello everyone,
I'm new to behavioir designer and I'm trying to create my first own task. I'm currently trying to alter the patrol task so the agent walks back and forth between the waypoints and not in a circle. So far so good... but I encountered a problem and I just can't wrap my head around what is going wrong...

I copied the code from the patrol task script and renamed the class.
And now I'm trying to feed my waypoints into the task. I have a WayPointManager Script which gives me the list of waypoints in my scene.
At first I tried to call the function on the WayPointManager Script to get the list of waypoints in the OnStart method on the task. As soon as
I start the scene, I get an error message:

1660319397852.png

Then I added the waypoints variable to the shared variables inside the behaviour designer.

1660319436913.png 1660319458629.png

And I added a script to my gameobject which sets the waypoints on Start.

C#:
public class EnemyController : MonoBehaviour{
    void Start()
    {
        BehaviorTree behaviourTree = GetComponent<BehaviorTree>();
        behaviourTree.SetVariableValue("waypoints", WayPointsManager.instance.GetWayPointsList()); 
    }
}

On runtime I can see the waypoints are correctly added to the shared variable. But I still get the same error message from the beginning.
The error messages occures as soon a the task tries to access the waypoints...


C#:
public override void OnStart()
        {
            base.OnStart();

            // initially move towards the closest waypoint
            float distance = Mathf.Infinity;
            float localDistance;
            for (int i = 0; i < waypoints.Value.Count; ++i)
            {
                if ((localDistance = Vector3.Magnitude(transform.position - waypoints.Value[i].transform.position)) < distance) // here the error occures
                {
                    distance = localDistance;
                    waypointIndex = i;
                }
            }
            waypointReachedTime = -1;
            SetDestination(Target());
        }

When I add the waypoints manually from the scene to the behaviour tree everything works fine... but I need to add the waypoints on runtime, because the gameobject with the bt is a prefab

Could it be a "race condition" and waypoints are assigned after the task has started?
I would say no. I disabeld the "Start When Enabled" option on the bt. Then I added the waypoints and after that I enabled the bt and got the same outcome.

C#:
void Start()
{
    BehaviorTree behaviourTree = GetComponent<BehaviorTree>();
    behaviourTree.SetVariableValue("waypoints", WayPointsManager.instance.GetWayPointsList());
    behaviourTree.EnableBehavior();
}

UPDATE:
I just tried to use the original patrol task from the movement pack and changed assigned the waypoints variable to the shared variable. And then added the waypoints on runtime. I get the same error.
 
Last edited:
Are you changing scenes? Are the variables set correctly within the editor window?

Based off of that error the references that you assigned have been destroyed and are no longer valid.
 
Are you changing scenes? Are the variables set correctly within the editor window?

Based off of that error the references that you assigned have been destroyed and are no longer valid.
I don‘t change the scene and as far as I can tell everything is setup correctly
 
I will try to describe what I did in detail....
At first I created a new variable as a GameObjectList in Behaviour Designer

1660372085218.png

Then I changed the waypoints variable in the inspector of the patrol task to use the shared variable

1660372199976.png

My GameObject, which uses the behaviour tree has only 4 components (besides the transform)...
a box collider
a nav mesh agent
the behaviour tree component
and my enemy controller script
The behaviour tree is set to NOT start when enabled, because I wanted to make sure that I can set the waypoints before the behaviour starts.
This is done in my enemy controller script.
C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using BehaviorDesigner.Runtime;

public class EnemyController : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        BehaviorTree behaviourTree = GetComponent<BehaviorTree>();
        behaviourTree.SetVariableValue("waypoints", WayPointsManager.instance.GetWayPointsList());
        behaviourTree.EnableBehavior();
    }
}

This is all what's inside my enemy controller script, nothing more.

My WayPointsManager script is on another game object in the scene and this is the script:

C#:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class WayPointsManager : MonoBehaviour
{
    [HideInInspector]
    public static WayPointsManager instance;
    public List<GameObject> waypoints;

    void Singelton()
    {
        if (instance != null && instance != this)
            Destroy(gameObject);
        else
            instance = this;
    }

    private void Awake()
    {
        Singelton();
    }

    public List<GameObject> GetWayPointsList()
    {
        return waypoints;
    }
}

At the moment it just returns the list of GameObjects which are my waypoints. They are setup correctly in the inspector.

1660372750828.png

My GameObject with the behaviour tree and my WayPointManager are in the scene right from the start... I don't change scenes in any way.
There are no other scripts in the scene. I deactivated everything else to see if there is something messing around .... but it doesn't look like it.
So at the moment I have only those two game objects active (WayPointManager and my GO with the behaviour tree), the waypoints gameobjects, a directional light, a camera and a plane which is the floor with the nav mesh...
 
Last edited:
And another detail.... when I start the scene the waypoints are visible in the inspector of the behavioir tree component....

1660379550407.png
 
Since the waypoints are reflected correctly in the editor that means that it has been properly loaded in your shared variable. What line is causing the exception?
 
Since the waypoints are reflected correctly in the editor that means that it has been properly loaded in your shared variable. What line is causing the exception?
looks like it is this line in the Patrol Task Script:

C#:
 public override void OnStart()
 {
     base.OnStart();

     // initially move towards the closest waypoint
     float distance = Mathf.Infinity;
     float localDistance;
     for (int i = 0; i < waypoints.Value.Count; ++i) {
         // THIS LINE HERE
         if ((localDistance = Vector3.Magnitude(transform.position - waypoints.Value[i].transform.position)) < distance) {
             distance = localDistance;
             waypointIndex = i;
         }
     }
     waypointReachedTime = -1;
     SetDestination(Target());
 }

this is the whole error message...

1660543130172.png
 
My guess is that your singleton is being reinstantiated at some point and causing the problem. I just recreated your setup and didn't have any issues. I've attached what I did so you can compare.
 

Attachments

  • Waypoints.unitypackage
    6.9 KB · Views: 1
My guess is that your singleton is being reinstantiated at some point and causing the problem. I just recreated your setup and didn't have any issues. I've attached what I did so you can compare.

Ok I found out what was causing the problem... the "Restart when complete" option... when I set it to true I get the error immediately.

EDIT: Ok it was not the problem. It worked only once... after stopping the play mode and restart it... I got the same error -.-
 
Last edited:
In that case your singleton is likely different from when the tree started versus when it restarted. I just tried restarting the tree with the repro scene above and didn't get any errors.
 

Attachments

  • waypoints.unity
    20.1 KB · Views: 0
Top