Using properties of an object makes the object null


All the example data is from hovering over the code in chooseAction()

		id: Ogre Peasant M,
		pos: [object Object],
		targetPos: [object Object],
		team: ogres,
		type: peon

I am grabbing array[0].targetPos and storing it into a var targetpos.

targetpos: Object {
	x: 7.622114134719595,
	y: 28.490717974491417,
	magnitude: <function>,
	heading: <function>,
	distance: <function>,
	dot: <function>,
	equals: <function>,
	copy: <function>,
	distanceSquared: <function>

If I add any line after:

var targetpos = array[0].targetPos;

such as:


it gives an error:

cannot read property 'x' of null

This also occurs if I use the original array[0].targetPos.x as well


Is it possible that sometimes targetPos is null? Peons/peasants don’t always have a targetPos
and it only has to be null and throw an error on one frame for one of them during the whole simulation.


just add a if (targetpos) { around your code that uses targetpos. targetpos can and will be null i think. if i’m reading it right targetpos is the position of the enemy / friend’s current target.