This site is currently in read-only mode during migration to a new platform.
You cannot post questions, answers or comments, as they would be lost during the migration otherwise.
+1 vote
# Character look at mouse position
if playerTarget == null:
    rayHit.y = player.translation.y
    player.look_at(rayHit, Vector3.UP)

# Character look at target
    mouseTarget = playerTarget.transform.origin
    mouseTarget.y += 1.5
    player.look_at(mouseTarget, Vector3.UP)
    gun.look_at(mouseTarget, Vector3.UP)

This is supposed to check if the mouse is pointed at an enemy. If so, player looks at enemy, if not, it just looks at the mouse position. Problem is every now and then, when testing the game, i get a null instance error after killing an enemy saying that playerTarget cant be found because its a null. Any ways to fix this?

Godot version
in Engine by (16 points)

2 Answers

0 votes

This is the only way to check for node, that might disappear during checking or iterating.

by (8,188 points)

Mind giving a sample. i tried it but gdscript cant find the function.

0 votes

I ran into the same issue, and it went away if I used is_instance_valid instead of != null

by (14 points)
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.