Implementing RandomPath into the script

Hi, I’m trying to make my npcs run randomly when the player is not within the distance given.
public void FixedUpdate () { if (Vector3.Distance (transform.position, target.position) > MaxDist) { seeker.StartPath (randompath, OnPathComplete); } }
randompath = new RandomPath (transform.position, LENGTH);
When I try it like this I get the errors “This constructor is obsolete. Please use the pooling API and the setup methods” and “object reference not set to an instance of an object”.
but when I replace it to randompath = RandomPath.Construct (transform.position, LENGTH);
it says the path has an invalid state. What am I doing wrong?

Huh, invalid state… that’s odd.
The second try (with the call to “Construct”) is exactly the way you should call it.
In fact the Path Types example scene does this:
RandomPath rp = RandomPath.Construct (start.position,searchLength, OnPathComplete); rp.spread = spread; rp.aimStrength = aimStrength; rp.aim = end.position;

Do you think you could post the exact error message it gives you?

Hm… are you constructing the path instance once and then calling StartPath with it repeatedly? It looks like that.

Each path instance should only be used for a single StartPath call (so you need to create a new one each time), and I also recommend that you wait until it has completed until you start the next one.
You can check that using

Well when I put in your fix it removes the original errors which were
but the npc is still staying still.

But I have
else if (Vector3.Distance (transform.position, target.position) > MaxDist) { path = null; } } for setting the path to null if the target is too far away
public void FixedUpdate () { if (seeker.IsDone ()) { seeker.StartPath (randompath, OnPathComplete); } }

It seems just starting a new path isn’t working

I still need help, if you can find an answer. :slight_smile:


You need to instantiate a NEW path object every time you start a path, you cannot reuse the old one.

Which is basically what the error you get says:
The path has an invalid state. Expected PathState.Created found <x> Make sure you are not requesting the same path twice

How exactly would I instantiate a new path?

Using the RandomPath.Construct method.
RandomPath rp = RandomPath.Construct (start.position,searchLength, OnPathComplete);
That will create a new path instance (or get it from a pool if possible).