NULL reference exception when applying penalty

Hi, I am making a RTS and I tried this so that the path generated avoids units. However, my units aren’t moving at all.

I called OnStop() in the Awake(). Whenever I set a new destination, I call OnMove() and then set CanMove to true. If I dont call OnStop() nor OnMove() my units move.

Could you help me with this problem? Not sure why it ain’t working.

if (hit.transform.tag == "Terrain") { mover.objetivo = targetPoint; mover.OnMove(); mover.canMove=true; }

Here are the functions posted in the link:

` public void OnStop() {
node = AstarPath.active.GetNearest(transform.position).node;
Bounds bounds = new Bounds (new Vector3(node.position.x,node.position.y,node.position.z), new Vector3 (0.1f, 0.1f, 0.1f));
guo = new GraphUpdateObject(bounds);
guo.addPenalty = Penalty;
guo.requiresFloodFill = false;
guo.resetPenaltyOnPhysics = false;
guo.updatePhysics = false;
AstarPath.active.UpdateGraphs(guo);
}

public void OnMove() {
	if (node.Penalty >= Penalty) {
		Bounds bounds = new Bounds(new Vector3(node.position.x,node.position.y,node.position.z),new Vector3(0.1f,0.1f,0.1f));
		guo = new GraphUpdateObject(bounds);
		guo.addPenalty = -Penalty;
		guo.requiresFloodFill = false;
		guo.resetPenaltyOnPhysics = false;
		guo.updatePhysics = false;
		AstarPath.active.UpdateGraphs(guo);
	} else {
		Debug.LogWarning("Invalid node penalty " + node.Penalty);
	}
}`

I’m getting NULLReference exceptions in:

node = AstarPath.active.GetNearest(transform.position).node;

and

if (node.Penalty >= Penalty)
Thanks in advance

Well the node variable obviously isn’t set to anything when OnMove is called. So the OnMove function is probably being called before OnStop in some cases.

It seems the OnStop() function isn’t updating the Penalty.

public void OnStop () { Debug.Log ("OnStop()"); node = AstarPath.active.GetNearest (transform.position).node; Bounds bounds = new Bounds (new Vector3 (node.position.x, node.position.y, node.position.z), new Vector3 (4f, 4f, 4f)); guo = new GraphUpdateObject (bounds); guo.addPenalty = Penalty; guo.requiresFloodFill = false; guo.resetPenaltyOnPhysics = false; guo.updatePhysics = false; AstarPath.active.UpdateGraphs (guo); Debug.Log ("Node Penalty: " + AstarPath.active.GetNearest (transform.position).node.Penalty); }

I am getting “Node Penalty: 0”.

I am also using that Debug in the update function and the Node Penalty is always 0. It is never update.

Hi

Graph updates are not applied immediately because pathfinding might be running at the same time. If you want to force graph updates to run, you must use
AstarPath.active.FlushGraphUpdates ();

See: http://arongranberg.com/astar/docs/class_astar_path.php#a1af9d7159ad315612d643fec03afa5bb

Also. node.position is not a Vector3, it is an Int3, you need to cast it to a Vector3 to use it like a world position.
Vector3 nodePosition = (Vector3)node.position;

If you just want to add penalty to a single node, you can however just do
GraphNode node = ... node.Penalty += 10000;

Thanks a lot :heart:
You have been a great help. Now my units dodge other stationary units. I am calling
AstarPath.active.FlushGraphUpdates (); every 1000 updates. It is working as I wanted but as It says in the documentation, the fps drop from 60 to 30 or less for one or two seconds when many agents stop.

Thanks a lot really, you saved me a lot of time.