How do you change the texture of a child node button- Null Instance Error

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

I am trying to run a script to change the texture of one of my textured buttons (right now just in the ready function but eventually on signals); however, I keep getting the error message

Attempt to call function ‘set_normal_texture’ in base ‘null instance’ on a null instance.

Based on making small changes it seems like my code is not recognizing my Button1 , but I am not sure why.

extends CenterContainer


func _ready():
	$Button1.set_normal_texture("res://Images/PeiceTile.png")

My nodes are a CenterContainer which containsHBoxContainer which contains Button1, Button2,…, Button 7.

Thank you in advance to anyone who is able to help me!

:bust_in_silhouette: Reply From: Zylann

Try with $HBoxContainer/Button1 (or whatever your hboxcontainer name is). Nodes are accessed by path, not just by name.

Thank you so much! I tried $HBoxCotainer.Button1 earlier but obviously I had the syntax wrong!

Also note to anyone else who looks at this in the future, i also needed to preload the texture to get this to work so my final code is

extends CenterContainer


func _ready():
	var PeiceTile = load("res://Images/PeiceTile.png")
	$HBoxContainer/Button1.set_normal_texture(PeiceTile)

Telley | 2020-03-19 23:18