Using Godot 4, following a tutorial but the tutorial is in GDScript. It is hard to find one for C# so I am trying to convert it as I go.

I made a scene called Coin and I have a scene called Main which is my main scene.

I want to instantiate a coin onto my scene.

Here's the GDScript:

var c = coin_scene.instantiate()
c.someprop = somevalue

This is my C# code:

[Export] public PackedScene CoinScene; // In my Main scene, I drag the coin.tscn onto it

// Ready method:
var coin = CoinScene.Instantiate(); 
AddChild(coin); // This adds the coin to my scene but at 0,0 ie default.
coin.someprop = x // Error since `coin` is not an instance of `coin.cs`

The coin.cs file is a script attached to my coin scene. However I can't find a way to access it.

I have seen some people say you can just cast it, but it just becomes null if I do var x = coin as coin;. The underlying type of the coin object seems to be Node2D even though the Instantiate() method returns a Node.

If anyone could shed some light on the hierarchy of what exactly is going on here it'd be appreciated, as resources for C# are incredibly rare.


1 Answer 1


The problem here was that the root node of the scene was defined as a Node2D, but the script was extending Area2D.

The mismatch meant that godot couldn't incorporate the script into the Node2D, which caused the problem.

Using Rider as the debugging application, the mismatch didn't appear as either an error or a warning within the build output or console. When I ran the project directly within Godot, it appeared as an error in the Debugger output.

Ensuring that the root node of the scene was an Area2D, same as the script, fixed the issue and I was able to cast using this code:

var coin = CoinScene.Instantiate<coin>();
coin.someprop = x; // works, coin.cs is the script attached to the root node in my coin scene.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .