Add TouchScreenButton in script

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

Hi I want to add a TouchScreenButton in my code, this is my code

var StartBtn = TouchScreenButton.new()
get_parent().add_child(StartBtn)
var normal_btn = preload("res://Load/startBtn.png")
StartBtn.normal.set_texture(normal_btn)

when i run this i got an issue
Attempt to call funtion ‘set_texture’ in base ‘null instance’ on a null instance, how can i solve it?

:bust_in_silhouette: Reply From: joaquin-montesdeoca

Hi, you should replace:

StartBtn.normal.set_texture(normal_btn)

by:

StartBtn.normal = normal_btn

Or if you still want to use the setter method, according to the documentation, the equivalent method for setting the normal property is set_texture

StartBtn.set_texture(normal_btn)

Yeah, I know, it probably should be called set_normal or set_normal_texture, but it is what it is.

Thanks, it works like this StartBtn.normal = normal_btn

MarcoAAG | 2021-04-01 00:13