The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes

Hi all,

I'm doing this:

var entity = ball.get_collider()
if entity.get_name() == "bat":
    #do stuff

and on this line if entity.get_name() == "bat": I'm getting this error:
Invalid call. Nonexistent function 'get_name' in base 'Nil'.

So I'm assuming that entity doesn't have anything in it. I find that strange though as it only comes into play AFTER the if statement. So how can that be?

I must be doing something wrong so I'm hoping someone can shed some fresh eyes on this for me.

Thanks so much....

in Engine by (824 points)

2 Answers

+1 vote
Best answer

Easy to fix. Basically, a single if can spot if entity is empty.

var entity = ball.get_collider()
if entity: # or "if entity != Nil:"
    if entity.get_name() == "bat":
    #do stuff
by (372 points)
selected by

Thank you, that was super simple. Can't believe I didn't think of it.

+3 votes

You can use is_colliding() before that code. If that is true, then there is a collider.

Like this:

if ball.is_colliding():
    var entity = ball.get_collider()
    if entity.get_name() == "bat":
    #do stuff
by (388 points)

A quick thing I noticed! Looks like there still might be a change is_colliding() is true and get_collider() is null. It seems to happen sometimes if the body (ball in your case) is destroyed using queue_free() somewhere.

I am just using another condition, is get_collider(), and so far seems ok.

if ball.is_colliding():
    var entity = ball.get_collider()
    if entity and entity.get_name() == "bat":
    #do stuff

Thank you, that worked also, as did the answer from rredesigns. Much appreciated

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.