Invalid set index rotation degrees on base "Nil"

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

i’m trying to have my sword instance rotate to my character’s direction but when i try to set the sword’s rotation it gives me this error here’s the code:

#this is in the player script:

func instance_melee(_direction):
	var melee_inst = melee.instance()
	var rot_direction = _direction.angle()
	melee_inst.set_sprite("katana", rot_direction)
	connect("set_meleeWeapon_pos", melee_inst, "set_pos")

#this is in the melee_weapon script:

func set_sprite(weapon, rot):
	sprite.rotation_degrees = rot
	sprite.scale = Vector2(2,2)
	sprite.texture = load(melee_weapons_dict[weapon]["weapon_sprite"])
	sprite.hframes = melee_weapons_dict[weapon]["nb_frames"]


where is sprite set and what to?

Does it have a value?

Add a print(sprite) to your set_sprite function.

deaton64 | 2020-06-11 16:34

sprite is set to a sprite node that is a child of this area2d it print’s out null if i print it out

ROBOTOO007 | 2020-06-11 18:37

So, sprite isn’t set appropriately - it’s null - which is the problem. So, you need to find out why the variable doesn’t have the value you expect. If you need help, post the code that sets the variable, as well as what your scene tree looks like, and which node in the scene tree has the above script associated with it.

jgodfrey | 2020-06-11 18:42

:bust_in_silhouette: Reply From: ROBOTOO007

I found out the probloem i was simply calling this function before adding the instance of the melee weapon to my scene i fixed everything and it’s working
THANKS to jgodfrey for helping me realise this !!!