Godot - get_node: 找不到节点 - GDScript

Godot - get_node: Node not Found - GDScript

请帮忙!当我 运行 我的游戏标题页代码时,我不断收到此错误消息!我对戈多很陌生。这是场景树:

Image

TitleScreen - 控制节点

- ColorRect - ColorRect Node
- Menu - VBoxContainer Node
    - Label - Label Node
    - Buttons - VBoxContainer Node
        - PlayButton - Button Node
        - QuitButton - Button Node
- Fade - ColorRect

这是连接到播放按钮和退出按钮的 GDScript 代码:

extends Button

export(String) var scene_to_load
export(bool) var quit

func _ready():
    for button in $Menu/Buttons.get_children():
        button.connect("pressed", self, "_on_button_pressed", [button.scene_to_load])

func _on_button_pressed():
    get_tree().change_scene(scene_to_load)

当我 运行 说场景时,我得到:

get_node:找不到节点:Menu/Buttons

任何帮助将不胜感激:)

感谢您帮助我解决问题! My Title Screen After being fixed :)

我的游戏 Game

您说您已将 GDScript 代码连接到每个 Button,但看起来您应该将脚本连接到 TitleScreen 而不是因为 $MenuTitleScreen 而不是 Button.

您还需要将脚本修复为 extend Control 而不是当前 extend Button

编辑

不过,老实说,如果您只想通过按下按钮加载特定场景而不需要其他任何东西,您可以直接通过按钮直接更改场景 - 这只是一个脚本更改

extends Button

export(String) var scene_to_load
export(bool) var quit

func _ready():   
    self.connect("pressed", self, "_on_button_pressed")

func _on_button_pressed():
    get_tree().change_scene(scene_to_load)