Topic was automatically imported from the old Question2Answer platform.
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??
Code:
extends Node
@export var mob_scene: PackedScene
func _ready():
randomize()
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)
add_child(mob)
Error Message:
Did you set a packed scene for your exported scene in your inspector
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):
mermiHavuzu.append(Mermi.instantiate())
func fire1():
if mermiHavuzu.size() > 0:
var mermi = mermiHavuzu[mermiHavuzu.size() - 1]
mermiHavuzu.resize(mermiHavuzu.size() - 1)
get_parent().add_child(mermi)
func _on_fire_timer_timeout():
fire1()
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.
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.
Looks like you have to go into the UI and under inspector under (these days it says the script name), a new thing will show up that you have to assign: https://youtu.be/WEt2JHEe-do?t=2177