0 votes

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 isingroup method

in Engine by (120 points)
edited by

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

1 Answer

+1 vote

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")
by (29,034 points)

thanks i'll try that

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

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.