Godot Version
godot verson 4
I am trying to make a pick up and throw system for may game (about throwing enermies through windows). When is was trying to set my RigidBoby3d to my marker3d i tried this code:
if shape_cast.is_colliding():
shape_cast.force_shapecast_update()
if shape_cast.is_colliding():
var collider = shape_cast.get_collider(0)
if collider is RigidBody3D:
carried_object = collider
carried_object.freeze = true
carried_object.global_transform = pickup_position.global_transform
and I cant find anithing on this that works and i came here. It shows this error “Invalid access to property or key ‘global_transform’ on a base object of type ‘null instance’.” Please help me.
Ps. sorry for the spelling mistakes.
Your pickup_position is likely null, how do you declare this variable? Are you sure it’s assigned correctly?
1 Like
Programming languages can often complain if something might be null, so when i see a bug of that type i usually write:
if (pickup_position!=null) and (carried_object!=null):
carried_object.global_transform = pickup_position.global_transform
This may silence the error but it won’t solve the problem, generally it’s good if your computer throws an error when something goes wrong rather than ignoring problems. Some call this “Failing fast” and it helps to make errors visible so you can fix them now rather than continuing in a broken state where errors will be harder to fix and there will be more red-herring errors.
2 Likes
If theres a problem you should catch it in
else:
Smart_print ( "error pickup of carry position is null")
But often that isnt triggered, the compiler often throws an error if something would break if the object was null … this happens many times and the problem is actually non-existent because for example the function only gets called when an area is entered by a body in physics layer 2, and only i know that its just objects with the ‘pickup_position’ node in layer 2 …the compiler might not be informed of all the variables and failsafes.
In fact thats something really dumb in programming languages … they can crash but they sometimes wont build in case of an ‘unsafe’ variable.
Well the actual bug in this situation could be something completely different, but error checking the codes ensures the build works and you could even walk through the code to find the issue. Thats why they invented ‘try’ and ‘catch’ in the olden days.