Turning off player's collision

Godot Version

Godot_V.4.2.2

Question

I have a player and a bullet,and when player interact with bullet i want to turn off player’s collision, so it dosnt collide,i tried using get_node(“CollisionShape2D”).disabled = false
But it says
Invald set index ‘disabled’ (on base ‘null instance’) with value of type ‘bool’
Please help

Godot telling you that on base ‘null instance’ Your call to get_node() failed. You might have name or path incorrect. A typo maybe?

can i maybe use something else than CollisionShape2D,like can i use get_node for player and type player and not CollisionShape2D
will that work

When you don’t want something to interact, you can either turn off collisions or set up a check for what the collision is.

I’ve used both - in my helicopter demo I check what rockets are hitting to make sure the player isn’t affected by their own bombs or rockets.

What code did u used to turn off collision?

The way get_node works is, referencing from the location in your scene tree.
One way you can get exact path, is by using “/root/scenename”
It works just like path names in your file browser.
You can see your scene tree and find it’s address. But yes, your collisionshape is a child of the player, so if your not calling it from the player node, you will need to add “player/collisionshape”

I am assuming it’s being called from a node next to the player, so it would need be, “…/player/collisionshape”. But something that is better is “/root/scenename/player1/collisionshape”

1 Like

I used code to make the item less visible and disable the collision

        print_debug("Hide key")
		var key_sprite = get_node("key/Sprite2D")
		var key_coll = get_node("key/CollisionShape2D")
		key_sprite.modulate.a = 0.5
		key_coll.set_deferred("disabled",true)
2 Likes