Hide nodes with a button?

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

I’d like to know how to hide a node with a button? I’m basically a beginner, and there are almost no tutorials, so…anyway, I’d like an example script, and I’m wondering if the same code would also work for an ogg. sound file. If not, a script for that would help too. Many thanks!

:bust_in_silhouette: Reply From: p7f

Hi,
You mean that you want to hide for example a Sprite when clicking a button?
Then, i assume you have this tree structure:

Main (Node2D)
----Sprite
----Button

Then you can use the signal pressed() and connect it to a function on Main.gd script (can connect it via editor. Go to Node tab, select pressed() signal and click connect). The function created when you connect the signal via editor should look like this:

func _on_Button_pressed():
    $Sprite.hide()

Is that what you need?

P.S: you can also hide Node2D and anything that derives from it. And what do you mean by hiding a sound file? stopping it from beeing played?

“Target method not found! Specify a valid method or attach a script to target Node.”

CREEPERCodingStudios | 2019-01-08 23:42

Hi,
What is your tree structure? To what node is your script attached? May be you can share me your project and ill know how to help you better. Also, What method is rising that error?

p7f | 2019-01-08 23:46

How can I share you my project?

Anyway, here’s my tree structure:

Node2D
Sprite
Label
AudioStreamPlayer
Button

I tested what you said to hide() the Label, but I got that error message.

CREEPERCodingStudios | 2019-01-09 00:02

You can share the project with google drive or any other cloud storage service and post the link here. Maybe if you just copy your full script here i may able to see the problem better.

p7f | 2019-01-09 00:37

Your script is the only one I have in my project. But yeah, I’ll get you that link.

CREEPERCodingStudios | 2019-01-09 00:52

Well, this is strange, but the project that you shared me has no script attached to main scene. Here i share you the project with the script, which hides label when pressing button. Works in my pc.

MEGA

p7f | 2019-01-09 01:35

Oh, I think I see what’s wrong here. Wow. Anyway, this has taken a lot longer than it probably should have, and I feel stupid…but I think the problem was I didn’t attach the script to the main scene. How would I do that?

CREEPERCodingStudios | 2019-01-09 02:41

You just need to select the root node from tha main scene and click the add script button in the editor. Have you already completed the official “Your first game” tutorial? If not, i recommend you doing it step by step, it shows all basic concepts in an easy way.

p7f | 2019-01-09 02:45

did you get it working?

p7f | 2019-01-09 16:24

Yes! It works now. Sorry, I just went to sleep for the night. But yeah, it works great, the weird thing is that I just fiddled around with it, and ended up changing it back to what you said and now suddenly it works. I must have changed something without realizing it. But anyway, thank you! I appreciate it!

CREEPERCodingStudios | 2019-01-09 22:33

You are welcome! Glad to help. If it worked fine, you may selec the answer so others see its solved.

p7f | 2019-01-09 22:37