I made car selection menu

Godot Version 4.2.1

Question


load() eror Invalid call. Nonexistent function ‘instance’ in base ‘PackedScene’.
character_selection.gd
extends Node3D

func _ready():
load_car()

func load_car():
var car_scene = load(Game.car_list[Game.current_car_index])
var car_instance = car_scene.instance()
#add_child(car_instance)
$Character_pos.add_child(car_instance)

func _on_slt_btn_pressed():
if Game.current_car_index < 4:
Game.current_car_index -= 1
prew_car()

func _on_nxt_btn_pressed():
if Game.current_car_index > 0:
Game.current_car_index += 1
next_car()

func next_car():
get_child(Game.current_car_index).queue_free()
Game.current_car_index = (Game.current_car_index + 1) % Game.car_list.size()
load_car()

func prew_car():
get_child(Game.current_car_index).queue_free()
Game.current_car_index = (Game.current_car_index - 1 + Game.car_list.size()) % Game.car_list.size()
load_car()

Game.gd
extends Node

const car_list = [“res://Car Scene/Dochshe.tscn”,“res://Car Scene/Dochshe_little.tscn”,“res://Car Scene/Sedan.tscn”,“res://Car Scene/Van.tscn”]
var current_car_index = 0

Game.gd global

instance() is for Godot 3.x In Godot 4.x it was renamed to PackedScene.instantiate()

1 Like

Godot Version 4.2.1

E 0:00:07:0714 character_selection.gd:37 @ next_car(): Index p_index = 3 is out of bounds ((int)data.children_cache.size() - data.internal_children_front_count_cache - data.internal_children_back_count_cache = 3).
<C++ Source> scene/main/node.cpp:1529 @ get_child()
character_selection.gd:37 @ next_car()
character_selection.gd:34 @ _on_nxt_btn_pressed()