Unity Warning - You are trying to create a MonoBehaviour using the 'new' keyword

Is there a way to get rid of this warning?

You are trying to create a MonoBehaviour using the 'new' keyword.  This is not allowed.  MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
UnityEngine.MonoBehaviour:.ctor () (at ?)
Pathfinding.Drawing.MonoBehaviourGizmos:.ctor () (at ./Library/PackageCache/com.arongranberg.astar@4.3.61/Drawing/MonoBehaviourGizmos.cs:11)
Pathfinding.VersionedMonoBehaviour:.ctor () (at ./Library/PackageCache/com.arongranberg.astar@4.3.61/PackageTools/VersionedMonoBehaviour.cs:27)
Pathfinding.MonoModifier:.ctor () (at ?)
Pathfinding.FunnelModifier:.ctor () (at ./Library/PackageCache/com.arongranberg.astar@4.3.61/Modifiers/FunnelModifier.cs:60)
ProjectSerpens.Pathfinding.AstarPathHelper:FindPath (UnityEngine.Vector3,UnityEngine.Vector3,Pathfinding.ITraversalProvider) (at Assets/Scripts/Pathfinding/AstarPathHelper.cs:73)
ProjectSerpens.Pathfinding.PathRenderer:Update () (at Assets/Scripts/Pathfinding/PathRenderer.cs:60)

I’m going to bet that your AstarPathHelper:FindPath function does something it shouldn’t do. Like trying to run new FunnelModifier().

1 Like

You’re absolutely right :smiley:
But what’s the right way? I have a central location where I calculate my path (with modifier, traversalprovider) and then use this path to my my character or draw the path with a LineRenderer

You can have a central Seeker component (with attached modifiers). Then you can either use seeker.StartPath or use AstarPath.StartPath combined with a seeker.PostProcess in the OnPathComplete callback to run the modifiers.

Hi. I am not sure if I fully understand you. This is my approach

I have a AstarPathHelper where I currenlty calculate my path with the wrong new FunnelModifier(), so I made the following modifications

  1. I added a Seeker to my AstarPathHelper GameObject
  2. created a SerializedField for the Seeker and added a reference
  3. Modified my FindPath method
public Path FindPath(Vector3 fromPosition, Vector3 toPosition, ITraversalProvider additionalTraversalProvider)
{
    //var path = ABPath.Construct(fromPosition, toPosition);

    var path = _seeker.StartPath(fromPosition, toPosition);
    
    // BlockManager.TraversalProvider traversalProvider = new (blockManager, BlockManager.BlockMode.OnlySelector, obstacles);
    path.traversalProvider = additionalTraversalProvider;
    AstarPath.StartPath(path);
    path.BlockUntilCalculated();

    // FunnelModifier funnelModifier = new();
    // funnelModifier.Apply(path);

    return path;
}

The result is

Exception: The path has an invalid state. Expected Created found PathQueue
Exception: The path has an invalid state. Expected Created found PathQueue
Exception: The path has an invalid state. Expected Created found Processing
Exception: The path has an invalid state. Expected Created found Processing
Exception: The path has an invalid state. Expected Created found ReturnQueue
Exception: The path has an invalid state. Expected Created found ReturnQueue

at path.BlockUntilCalculated();

Hi

You should only call one of seeker.StartPath and AstarPath.StartPath. Not both.

1 Like

After playing around with it and writing a custom movement script, I was wondering how to use my precalculated path with AILerp?

I have a Global Pathfinder GameObject with a Seeker, FunnelModifier and a CustomScript where I calculate the path from the position of a fixed agend to a target:

private Path CalculatePath(Vector3 from, Vector3 to)
{
    UnityEngine.Debug.Log("Calculating path from " + from + " to " + to);
    var path = GetComponent<Seeker>().StartPath(from, to);
    path.BlockUntilCalculated();
    return path;
}

Then I set the path to AILerp, added to the agent

var path = CalculatePath(_agent.transform.position, _target.position);
_agent.GetComponent<AILerp>().SetPath(path);

It works, kinda. It seems that the Seeker of the agent now calculates its own path (without a modifier; no added modifier at the agent) and I see 2 path gizmos, one smooth and one angled and the agent follow the path calculated by its own seeker, the given path is ignored.