Hi, im trying to instantiate an object and im still gettin an error and im way too stupid to solve it myself (c#)

Godot Version

<3,5>

Question

the error :
PackedScene does not contain a definition for GlobalPosition, and no available GlobalPosition extension method was found that accepts the first argument of type PackedScene. (Is there a using directive or assembly link missing?)

the script:
using Godot;
using System;

public partial class spawn_test : Node2D
{

public override void _Ready()
{
	string cesta = "res://prefabi/odpad.tscn";
	PackedScene pack = GD.Load<PackedScene>(cesta);
	
	for(int i = 0; i < 5; i++)
	{
		PackedScene p = pack.Instantiate<PackedScene>();
		p.GlobalPosition = new Vector2(0,0);
		//AddChild(p);
		CallDeferred("add_child", p);
	}
}

After Instantiate, you set the type of the variable as PackedScene. However, its type should be Node2D or Node3D

thanks a lot sorry for bothering you but is there a way to convert PackedScene to Node2D or use Node 2D to instantiate with node2d because compiler keeps complaining about that node2d does not have definicion for instantiate
sorry im stupid and switching from unity to godot

something like

var p = (Node2D)pack.Instantiate();

Just a guess as I only do 3D, but it should work.

(sorry for all the edits, but first time using this forum :wink: )

1 Like

ok what even is this error: E 0:00:00:0055 parse_mapping: Unrecognized input string “8e4f” in mapping:
03000000790000000600000000000000,G-Shark GS-GP702,crc:8e4f,a:b2,b:b1,back:b8,dpdown:h0.4,dpleft:h0.8,dpright:h0.2,dpup:h0.1,leftshoulder:b4,leftstick:b10,lefttrigger:b6,leftx:a0,lefty:a1,rightshoulder:b5,rightstick:b11,righttrigger:b7,rightx:a2,righty:a3,start:b9,x:b3,y:b0,platform:Windows,
<C++ Error> Condition “true” is true. Continuing.
<C++ Source> core/input/input.cpp:1441 @ parse_mapping()

I have no idea where that error is from, probably unrelated to this.
However, I tried CallDeferred as you have it coded and it didnt work for me either.
But AddChild(p) does, and also I am on 4.2 Godot.

thanks a lot

Just a suggestion; the other day I figured out you can load a resource from its uid rather than its file location. That way, the resource will still load if you move files around.

You can find the uid on the first line of a .tscn file:
[gd_scene load_steps=8 format=3 uid="uid://p3qs6rrve2ep"]

Or right-click the .tscn file in the editor and click “Copy UID”.

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.