Position of instanced scene

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

Hello

I want to spawn enemies in random position, what I did in short was to instance my enemy scene into my main scene(that’s where I code the spawning thing) and when I try to set the position of my enemy scene it says there is extension method for it

PackedScene foodsource;
public Node foodinstance;
      public override void _Ready()
        {
            foodsource = GD.Load<PackedScene>("res://Food.tscn");
            foodinstance = foodsource.Instance();
        }
     public override void _Process(float delta)
      {
        foodinstance.position // ***that's where there is error***
       }

I would happy for some help(in c# if it’s possible)

c#I don’t know. if i’m not mistaken
add
add_child(foodinstance)

public Node foodinstance;
      public override void _Ready()
        {
            foodsource = GD.Load<PackedScene>("res://Food.tscn");
            foodinstance = foodsource.Instance();
            add_child(foodinstance) ## this gdscript

ramazan | 2022-09-08 22:01

Yeah, But I can’t access the foodinstance position

godotuser123 | 2022-09-09 09:19

try

{
  //foodinstance.position // ***that's where there is error***
  print(foodinstance) // this gdscript
  // or 
  print(foodinstance.position) // this gdscript
 }

ramazan | 2022-09-09 11:04

It doenst work because there is no position property for a Node node

godotuser123 | 2022-09-09 11:15

res://Food.tscn = ?
what is type?
node type ? sprite , kinematicbody , area … etc

sorry
node type = Node
change Node = Node2d

ramazan | 2022-09-09 11:23

the food is scene is area2d if thats what you mean

godotuser123 | 2022-09-09 11:29

print(foodinstance) // output ?

ramazan | 2022-09-09 11:34

Output: area2d bro

godotuser123 | 2022-09-09 11:43

???
everything is correct. I don’t understand why there is a problem
position = Position

ramazan | 2022-09-09 11:45

Same here bro I really dont know what is the problem

godotuser123 | 2022-09-09 11:50

try
position = gdscript
Position = c#

ramazan | 2022-09-09 11:56

Tried it and I also tried globalposition
Doesnt work either

godotuser123 | 2022-09-09 11:59

try
position = gdscript
Position = c#

GD.Print(GlobalPosition);

ramazan | 2022-09-09 12:00

It will print the position of the root scene

godotuser123 | 2022-09-09 12:06

PackedScene foodsource;
public Node foodinstance;
      public override void _Ready()
        {
            foodsource = GD.Load<PackedScene>("res://Food.tscn");
            foodinstance = foodsource.Instance();
        }
     public override void _Process(float delta)
      {
        //foodinstance.position // ***that's where there is error***
        foodinstance.Position;
       }

ramazan | 2022-09-09 12:08

??
Its the same code

godotuser123 | 2022-09-09 12:13

“P” uppercase
position <==> Position

ramazan | 2022-09-09 12:19

Doesnt work bro

godotuser123 | 2022-09-09 15:11

:bust_in_silhouette: Reply From: aXu_AP

You need to set the Position to something. Ie.

foodinstance.Position = new Vector2(GD.RandRange(-100f, 100f), GD.RandRange(-100f, 100f));

Sorry if I got some detail wrong, I haven’t done much C# on Godot.

But according to the editor

Foodinstance.Position 

Doesnt exist
There is no position property for a node

godotuser123 | 2022-09-09 22:58

:bust_in_silhouette: Reply From: juppi

Node doesn’t have a Position property:

You need a Node2D at least.

But instance() returns Node and not Node2D
I also tried to cast it

foodinstance = (Node2D)foodsource.Instance();

Even though now there is position property I can’t change it
the debugger says

cannot modify the return value Node2D.Position because it’s not a variable.

godotuser123 | 2022-09-11 14:18

That’s right. You have to do it like this:

myNode2D.Position = new Vector2(100, 100);

That’s because Position is a Property and not a public field.

public Vector2 Position { get; set; }

The property does the same like this:

private Vector2 _position;

public Vector2 GetPosition()
{
    return _position;
}

public void SetPosition(Vector2 position)
{
    _position = position;
}

GetPosition().x = 4711 gives me the same error.

juppi | 2022-09-12 14:49