• 2D
  • How can I fix this error "Invalid set index 'offset' (on base: 'null instance') with value of type'?

This is te script...

extends Node

export (PackedScene) var Mob
var score

func _ready():
	randomize()
	new_game()

func _on_Player_hit():
	pass # Replace with function body.

func game_over():
	$ScoreTimer.stop()
	$MobTimer.stop()

func new_game():
	score = 0
	$Player.start($StartPosition.position)
	$StartTimer.start()

func _on_MobTimer_timeout():
	# Choose a random location on Path2D.
	$MobPath/MobSpawnLocation.offset = randi()
	# Create a Mob instance and add it to the scene.
	var mob = Mob.instance()
	add_child(mob)
	# Set the mob's direction perpendicular to the path direction.
	var direction = $MobPath/MobSpawnLocation.rotation + PI / 2
	# Set the mob's position to a random location.
	mob.position = $MobPath/MobSpawnLocation.position
	# Add some randomness to the direction.
	direction += rand_range(-PI / 4, PI / 4)
	mob.rotation = direction
	# Set the velocity (speed & direction).
	mob.linear_velocity = Vector2(rand_range(mob.min_speed, mob.max_speed), 0)
	mob.linear_velocity = mob.linear_velocity.rotated(direction)


func _on_ScoreTimer_timeout():
	 score += 1


func _on_StartTimer_timeout():
	$MobTimer.start()
	$ScoreTimer.start()

You could try placing everything in _on_MobTimer_Timeout() within a check(if conditional) to see if the node MobPath exists.

You need to rename the node PathFollow2D to MobSpawnLocation. Or change your code to use PathFollow2D instead of MobSpawnLocation.

Yeah, I had the topic page loaded from before the image was posted so I missed it. As @DaveTheCoder said, that is definitely the thing to do.

Other mistake... Invalid call. Nonexistent function 'instance' in base 'Nil'.

@Megalomaniak said: You could try placing everything in _on_MobTimer_Timeout() within a check(if conditional) to see if the node MobPath exists. Can you explain me better?

You want to check that variant Mob is not empty or unassigned.

@Megalomaniak said: You want to check that variant Mob is not empty or unassigned.

How can I check?

something along the lines of

if Mob = -node type here-: or if Mob != Null:

before your line 26 and then indent what was line 26 to be within the if statement. Also indent the next line with the add_child(mob)