attempt to call function "is_in_group" in base "null instance"

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

here’s my code :

extends KinematicBody2D

var speed: = Vector2(300,300)
var direction: = Vector2(0,0)



func _physics_process(delta: float) -> void:
	direction = get_direction()
	var velocity = speed * direction
	velocity = move_and_slide(velocity)
	var object_colliding = get_node("RayCast2D").get_collider()
	if object_colliding.is_in_group("interractable_objects") && Input.is_action_pressed("interract"):
		print("cool")


func get_direction() -> Vector2:
	return Vector2(Input.get_action_strength("right") - Input.get_action_strength("left"),
					Input.get_action_strength("down") - Input.get_action_strength("up"))

it gives me an error when i launch my game for some reason there’s a problem with the is_in_group method

Formatted your code. You can do it by selecting it and then clicking the Code Sample button.

Zylann | 2020-02-26 13:55

:bust_in_silhouette: Reply From: Zylann

When Godot tells you in base "null instance", it means the object on which you call the function is null. Here, object_colliding is null.

One reason could be that get_node("RayCast2D").get_collider() did not return any collider, which means there is no collision. So you should check for that case before checking the group of the collider.

if object_colliding != null && object_colliding.is_in_group("interractable_objects")

thanks i’ll try that

ROBOTOO007 | 2020-02-26 14:23

the game doesn’t crash anymore but it still doesn’t print out anything

ROBOTOO007 | 2020-02-26 14:32

New problem then.
According to your code, for the print to happen, you need all of the following conditions to succeed:

  • The collider must be detected (non-null)
  • The collider must be in the interractable_objects group
  • The button bound to the interract action must be pressed

Check which ones are happening.

Zylann | 2020-02-26 19:00