• Godot HelpProgramming
  • [SOLVED] Why does the script component disappear on scene root Node2D after instantiating it?

Hello,

I have a weird problem with instantiating scenes. I have a player scene, which root node is simply a type of Node2D. That node is then attached to a Player.gd script. The player script extends Actor.gd which then extends Node2D. I use inheritance and gdscript's _init() -function.

So my problem is that whenever instantiating the player scene the Player.gd is always null. I have tried attaching multiple different kind of scripts but they are always null.

Scene

Here you can see the script as null after instantiating

Welcome to the forums @Baserfaz!

Are you assigning the script in the instanced scene file or in the scene its instanced in? I would check to make sure the instanced scene file has the script attached, and then double check the scene its instanced in to see if the script is there. If not, you should be able to scroll down to the script category and there should be a little reset button. If the script is still not there and you cannot reset the property, then I would see if making a new instance of the scene (not a new scene, just a new instance) fixes the issue.

The script is attached to the root node in the scene file. It is still missing when I call instance() function. The Player.gd _init() is never called.

Ok, so I got it working now, but not as I wanted it to.

I think there is a bug in the instance process if the root node has _init -function with parameters, it does not even matter if there is inheritance.

This works:

And this is never getting called:

The quick fix for this is to NOT use gdscript _init() -function but instead use your own init() -function.

Huh, strange and not what I would have expected. I'm glad you found what was causing the issue though, and thanks for sharing it!

2 years later