Invalid set index ‘position’ (on base: ‘Nil’) with value of type ‘Vector2’

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

The following line of code got me the error mentioned above:

bullet_instance.position = get_global_position()

It’s supposed to instance a bullet at the gun’s position. bullet_instance is defined like so:

var bullet_instance = _ready()

Thank you in advance, especially since this is for a game jam

There is definitely some misunderstanding in the above code. First, the _ready() function isn’t intended to be called by your code. It’s automatically called by the engine when the scene is, well, ready.

Further, the _ready() function doesn’t return anything (it’s a void function), so assigning its result to your bullet_instance variable will certainly cause the variable to be null, as indicated by the reported error.

Without more code, it’s difficult to provide additional assistance.

jgodfrey | 2022-08-24 14:36

:bust_in_silhouette: Reply From: SteveSmith

var bullet-instance = ready() is technically meaningless, and will result in bullet_instance being null. You are then trying to set the position of a null object, thus you get your error.