Hi
When you add an instance to the game, you then need to call addchild before it will show up in the world. Until you do this, the instance exists in your code and you can make changes to prepare it but it won't be active the game. In your first example you only addchild when onplayer1pressed and then immediately make it invisible again. So, I guess that's why you don't see it. It looks like you got it right in the spawnenemy1() code
I am getting confused with all the small pieces of code that you are sending - it is hard to know what code is in what script.
In this example code which you shared:
var Enemy1ins = preload("res://Enemy/Enemy1.tscn")
var Enemy1instance = Enemy1ins.instance()
func _on_player1_pressed():
add_child(Enemy1instance)
Enemy1isntance.visible = false
if you want to make that work I believe you need to...
1) Make sure these are at the top of your script before the function definitions
var Enemy1instance # This needs to be a member variable (declared outside a function) so it persists and can be used in all functions.
var Enemy1ins = preload("res://Enemy/Enemy1.tscn")
2) Ensure you are including add_child for the instances. You could add this to your _ready
func _ready()
Enemy1instance = Enemy1ins.instance()
add_child(Enemy1instance)
If you run that, the Enemy should show up. If it does not, something else is wrong which we need to fix first.
Assuming that works, then you can try
func _on_player1_pressed():
Enemy1instance.visible = false
However... if you have a bunch of similar enemies, like you just shared in your last example with spawn_enemy1 it's probably going to be easiest to use a Group. To do this, ensure your code is correctly showing Enemies, then add your Enemy scene(s) to a Group, maybe called Enemy. You can do that in the editor (no code required) - instructions here
When you have a group, and you are successfully showing enemies on screen then you can easily call functions on every member of the group like this
In your enemy script, try this sample function (you might already have this from a previous question)
func hide_for_time(time):
visible = false
yield(get_tree().create_timer(time), "timeout")
visible = true
This makes the enemies invisible, then waits for the duration of the time variable, then makes them visible again
In your player script, add
func _on_player1_pressed():
get_tree().call_group("Enemy", "hide_for_time", 2)
This gets every member of the "Enemy" Group and calls a function called "hidefortime" on the every member. The function is called with the parameter "2" (duration).