Why is .instantiate() returning a null value on a scene?

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

I’m fairly new to Godot so I’m following a tutorial that lots of people recommend. Its by GDQuest (link: https://www.youtube.com/watch?v=WEt2JHEe-do&t=2392s) but it was made in an older version of Godot. I was warned about this but told that the comments would have the updated code. But for this particular instance it does not. The tutorial said to use .instance(), but that command no longer exists. I put in .instantiate() becuase it seemes like the closetest thing and later verified it was right. But it keeps returning “Cannot call method ‘instantiate’ on a null value”. Any idea whats wrong??


extends Node  
@export var mob_scene: PackedScene

func _ready():

func _on_mob_timer_timeout():
var mob = mob_scene.instantiate()
var mob_spawn_location = $MobPath/MobSpawnLocation
mob_spawn_location.progress_ratio = randf()

mob.position = mob_spawn_location.position

var direction = mob_spawn_location.rotation + PI / 2
direction += randf_range(-PI / 4, PI / 4)
mob.rotaion = direction

var velocity = Vector2(randf_range(mob.min_speed, mob.max_speed), 0)
mob.linear_velocity = velocity.rotated(direction)

Error Message:

Did you set a packed scene for your exported scene in your inspector

Venex2004 | 2023-04-25 06:35

:bust_in_silhouette: Reply From: Game_gulf

i see some confusion i leave you a working code fix it yourself

var Mermi  = load("res://Scenes/bullet.tscn")

func mermi_topla():
	var havuzBoyutu = 1000
	for i in range(havuzBoyutu):

func fire1():
		if mermiHavuzu.size() > 0:
			var mermi = mermiHavuzu[mermiHavuzu.size() - 1]
			mermiHavuzu.resize(mermiHavuzu.size() - 1)

func _on_fire_timer_timeout():
:bust_in_silhouette: Reply From: FranzPantalon

Hi there,

I’ve been struggling with the same problem at this very moment, and found out how to solve it.

After you exported the mob_scene property, this property of the main is now visible in the inspector, when you select the main node.

From there, you shall set the property with a packed scene to specify to the main, what a mob scene “is”. Therefore, set the mob_scene property in the inpector’s GUI by loading the scene ‘mob.tscn’ you created previously in the tutorial.

Et voilà :slight_smile:

:bust_in_silhouette: Reply From: stormreaver

You’ve told Godot that mob_scene will be a PackedScene when loaded, but you never load anything into mob_scene. Until mob_scene is initially assigned a value via load or preload, it will be null.

1 Like