Invalid operands 'Vector3' and 'Nil' in operator '*'

Godot Version

Question

I am trying to make an enemy move towards a point depending on its current state with this function. However every time I try to run the game I get the same error: Invalid operands ‘Vector3’ and ‘Nil’ in operator '*.

func MoveTowardsPoint(delta, speed):
	var targetPos = navigation_agent_3d.get_next_path_position()
	var direction = global_position.direction_to(targetPos)
	faceDirection(targetPos)
	velocity = direction * speed
	move_and_slide()
	if(playerInEarshotFar):
		CheckForPlayer()

The only time I call this function is in another function _process.

func _process(delta):
	CheckForPlayer()
	match currentState:
		States.patrol:
			if(navigation_agent_3d.is_navigation_finished()):
				currentState = States.waiting
				patrol_timer.start()
				return
			MoveTowardsPoint(delta, patrolSpeed)
			pass
		States.chasing:
			if(navigation_agent_3d.is_navigation_finished()):
				patrolTimer.start()
				currentState = States.waiting
			navigation_agent_3d. set_target_position(camera_3d.global_position)
			MoveTowardsPoint(delta, ChaseSpeed)
			pass
			
		States.hunting:
			if(navigation_agent_3d.is_navigation_finished()):
				patrolTimer.start()
				currentState = States.waiting
			MoveTowardsPoint(delta, patrolSpeed)
			pass
		States.waiting:
			CheckForPlayer()
			pass
	pass

The line giving the error is:

velocity = direction * speed

I have no idea how to fix the error and help would be greatly appreciated.

if you run this code you will get the same error

extends Node2D
var x
var v = Vector3(1,1,1)
func _ready():
	print(v * x)

make sure that

patrolSpeed

is set as something

1 Like

As HexY said your speed is null (Nil), meaning it has no value assigned to it.

The error:
Invalid operands ‘Vector3’ and ‘Nil’ in operator '*. says:

Your multiplication has invalid values. One of them is a vector3 and one of them is null. So your speed must be null.

Just keep that in mind for future errors. It’s helpful to understand the errors Godot shows.

func MoveTowardsPoint(delta, speed):
var targetPos = navigation_agent_3d.get_next_path_position()
var direction = global_position.direction_to(targetPos)
faceDirection(targetPos)
velocity = direction * speed
move_and_slide()
if(playerInEarshotFar):
CheckForPlayer()

Global position may equal target position when character is at the end of the path. Direction to does (A-b). normalized()

IDK what happens if a zero vector is normalized? It says a near zero vector will may point in an incorrect direction. But an absolute zero?