Sharing NavMeshAgent (customClass) Across Actions in Behavior Designer

Antesy

New member
Hi,

I would like to share my NavMeshAgent across various actions in my project. I've implemented the following code in BehaviorTree.cs & SharedNavMeshAgent.cs :

SharedNavMeshAgent.cs :

Code:
using UnityEngine.AI;
using BehaviorDesigner.Runtime;


[System.Serializable]
public class SharedNavMeshAgent : SharedVariable<NavMeshAgent>
{
    public static implicit operator SharedNavMeshAgent(NavMeshAgent value)
    {
        return new SharedNavMeshAgent { Value = value };
    }
}


BehaviorTree.cs :

Code:
using UnityEngine;


namespace BehaviorDesigner.Runtime
{
    [AddComponentMenu("Behavior Designer/Behavior Tree")]
    public class BehaviorTree : Behavior
    {
        public class CustomClass
        {
            public SharedNavMeshAgent sharedNavMeshAgent;


        }
}

1714409490784.png


However, I have multiple actions and I'm unable to link the NavMeshAgent defined in the behavior tree (via the variables window). How can I achieve this?

1714409579121.png
Currently, the options I see are "dynamic", "none", or manually setting it, which I would prefer to avoid. Do you have any suggestions? It seems to be a type-related issue.

EDIT :
I've also tried this:
Code:
public class Chasing : Action
{
    public SharedTransform sharedTransform;
    public SharedNavMeshAgent SharedNavMeshAgent;
    public SharedTransform target;
    public CustomClass test;


    public class CustomClass
    {
        public SharedNavMeshAgent sharedNavMeshAgent;


    }
}
Code:
using UnityEngine;


namespace BehaviorDesigner.Runtime
{
    // Wrapper for the Behavior class
    [AddComponentMenu("Behavior Designer/Behavior Tree")]
    public class BehaviorTree : Behavior
    {
 [System.Serializable]
        public class SharedCustomClass3 : SharedVariable<SharedNavMeshAgent>
        {
            public static implicit operator SharedCustomClass3(SharedNavMeshAgent value) { return new SharedCustomClass3 { Value = value }; }
        }
    }
}
And this, but I encountered an error that says:"Accessibility: the base class SharedVariable<NavMeshAgent> is less accessible than the class BehaviorTreeSharedCustomClass3."
Code:
[System.Serializable]
        public class SharedCustomClass3 : SharedVariable<navmeshagent>
        {
            public static implicit operator SharedCustomClass3(SharedNavMeshAgent value) { return new SharedCustomClass3 { Value = value }; }
        }




Thank you for your assistance.
 
Last edited:
You do not need the BehaviorTree subclass of Behavior that has the CustomClass. The Behavior Designer BehaviorTree component will store the list of SharedVariables/tasks for you.

From there it looks like you have SharedNavMeshAgent wrapped in another class which is why there is that foldout within the variables tab and why your SharedVariable within the Chasing task cannot be found. If you do not place the SharedNavMeshAgent in another class the variables tab will display it directly without the foldout and you will be able to select it within the Chasing class.
 
Top