Search code examples
game-developmentgodotgdscript

Unable to use get_node() to locate a Label Child (GD script)


I am trying to obtain my Label node "ON" through a script I've attached to its parent "Shadow Settings" (which is also a label). Problem is, I keep getting this error,

E 0:00:08.208   get_node: (Node not found: "ON" (relative to "/root/ShadowsSettings").)
  <C++ Error>   Condition "!node" is true. Returned: __null
  <C++ Source>  scene/main/node.cpp:1371 @ get_node()
  <Stack Trace> Shadows Settings.gd:15 @ _process()

And here is my Code,

extends Label

var Shadows_On = true

func _process(_delta):

    if not Shadows_On:
        
        get_node("ON").visible = false
        
        get_node("OFF").visible = true

    else:
        
        get_node("ON").visible = true
        
        get_node("OFF").visible = false

func _on_SettingsButton_button_up():
    
    if get_parent().get_parent().get_parent().visible:
    
        if Shadows_On:

            Shadows_On = false

        else:
            
            Shadows_On = true

Please help me, I've been stuck for days trying to fix what you would think would be very simple to solve. 🙏


Solution

  • The problem is that you did not give the full node path to the get_node function. The easiest to solve this would be to export a variable so you can set its path via the editor.

    It would look something like this:

    export var label_path = "empty"
    onready var my_label = get_node(label_path)
    
    func _process(delta):
        my_label.visible = false
    

    Just remember to assign the label_path a value in the editor view.