How can I fix this eror: "Invalid call. Nonexistent function 'instance' in base 'Nil'."?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By David8184

This is the 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()

:bust_in_silhouette: Reply From: kidscancode

You haven’t assigned a value to the Mob variable:

export (PackedScene) var Mob

As the instructions say:

Click the Main node and you will see the Mob property in the Inspector under “Script Variables”.

You can assign this property’s value in two ways:

  • Drag Mob.tscn from the “FileSystem” panel and drop it in the Mob property .
  • Click the down arrow next to “[empty]” and choose “Load”. Select Mob.tscn.

Ok. Thank you very much.

David8184 | 2021-07-11 16:37