it keep reapeating showing error saying Cannot call method 'add_child' on a null value.

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

extends Node3D

var Room = preload(“res://scene/newScene/newRoom.tscn”)
var tiles = 40
var number_room = 50
var mins = 5
var maxs = 12

func _ready():
randomize()
create_rooms()

func create_rooms():
for i in range(number_room):
var post = Vector3(1,1,1)
var r = Room.instantiate()
var x1 = mins + randi() % (maxs-mins)
var z1 = mins + randi() % (maxs-mins)
r.create_room(post, Vector3(x1, 1, z1) * tiles)
$Room.add_child(r)

in different scene theres :
extends RigidBody3D

var size = Vector3()

func create_room(apos, asize):
position = apos
size = asize
var s = BoxMesh.new()
s.size = Vector3(size)
$CollisionShape3D.shape = s

error, Cannot call method ‘add_child’ on a null value.
how do i fix this

‘’$Room’’ doesnt exist your either freeded it or didnt add to your scene

horsecar123 | 2023-05-07 20:40

:bust_in_silhouette: Reply From: CollCaz

in the first line you have

var Room = preload("res://scene/newScene/newRoom.tscn")

then you try to

$Room.add_child(r)

what you should do is

Room.add_child(r)

$Room is null because there is no scene called Room in the tree

Edited to change addchild() calls to add_child(). Similar references in the OP are broken because the code was not formatted for the editor and the underscore in the function name is being misinterpreted by the forum’s markdown processor.

jgodfrey | 2023-05-10 18:39

thank you for fixing that up

CollCaz | 2023-05-10 22:14