How do I properly instance a node at the right location using c#?

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

So I’m currently following a tutorial by HeartBeast to spawn enemy ships for a space shooter game. He’s using GDScript and I’m trying to convert what I see to C# and have been doing an OK job up until now. My issue is that I’ve created an Enemy Spawner scene and am trying to create an instance of an enemy at the Enemy Spawner location but everything keeps spawning in the top left corner (0,0) and I can’t get anything to change no matter how hard I try to troubleshoot. This is what my EnemySpawner script looks like:

`public class EnemySpawner : Node2D
{
PackedScene Enemy = GD.Load(“res://Enemy.tscn”);
Node2D spawnPoints = new Node2D();
Node2D temp = new Node2D();
Godot.Collections.Array points;

// Called when the node enters the scene tree for the first time.
public override void _Ready()
{
    base._Ready();
    spawnPoints = GetNode<Node2D>("SpawnPoints");
}

public Vector2 getSpawnPosition()
{
    points = spawnPoints.GetChildren();
    points.Shuffle();
    temp = (Node2D)points[0];
    return temp.GlobalPosition;
}

public void spawnEnemy()
{
    var getSpawnPoint = getSpawnPosition();
    GD.Print(getSpawnPoint);
    Node2D enemy = (Node2D)Enemy.Instance();
    var main = GetTree().CurrentScene;
    main.AddChild(enemy);
    
}

public void OnTimerTimeout()
{
    spawnEnemy();
}

}`

I can provide more snippets of other scripts if need be. I have a feeling it has something to do with C# that I’m unfamiliar with. Any and all help is very much appreciated.

:bust_in_silhouette: Reply From: Wakatta

In the sample provided you never set the node’s position.
Additionally know that you should set the position after the node has been added to the scene

public void spawnEnemy()
{
    var getSpawnPoint = getSpawnPosition();
    GD.Print(getSpawnPoint);
    Node2D enemy = (Node2D)Enemy.Instance();
    var main = GetTree().CurrentScene;
    main.AddChild(enemy);
    
    # the missing code
    enemy.GlobalPosition = getSpawnPoint
}

Thank you for your reply. So I added ‘enemy.GlobalPosition = GetSpawnPosition();’ to the end of the spawnEnemy() method. I did have that in there initially but I guess it got deleted while I was troubleshooting unfortunately. However, I’m still getting the same effect of the enemy spawning at (0,0). When you say set the position after the node has been added to the scene, that should be happening with that last line you told me to add and nothing is happening so I’m not entirely sure why it’s still spawning at 0,0. I’ve been using GD.Print to check global positions of the points I want them to spawn at and they’re displaying correctly but the enemy isn’t spawning at their locations for some reason.

Jeric0h77 | 2023-03-04 05:38

So I found out the issue isn’t even with this script but actually my Enemy script. But, thanks to you I have found where my issue lies. Thanks so much for your help!

Jeric0h77 | 2023-03-04 06:05

Care to share for future reference or for others with the same problem

Wakatta | 2023-03-04 14:19