AbsolutePickle42
AbsolutePickle42

Reputation: 61

Godot: attempt to call function 'get_texture' in base 'null instance' on a null instance

I've got a texture painter prototype, but like the title says, I am getting the error message "attempt to call function 'get_texture' in base 'null instance' on a null instance" whenever I try to run the program. Script:

func _process(delta: float) -> void:
    var camera = get_parent().get_node("Camera")
    if camera != null:
        var from = camera.project_ray_origin(mouse_position)
        var to = from + camera.project_ray_normal(mouse_position) * raycast_length
        var space_state = get_world().direct_space_state
        var result = space_state.intersect_ray(from, to)
        if result:
            var children = result.collider.get_children()
            for child in children:
                if child is MeshInstance:
                    if selected_mesh != null:
                        selected_mesh.material_override = null
                    selected_mesh = child
                    selected_mesh.material_override = SpatialMaterial.new()
                    selected_mesh.material_override.albedo_texture = selected_mesh.get_surface_material(result.shape).get_texture("albedo_texture")
                else:
                    if selected_mesh != null:
                        selected_mesh.material_override = null
                        selected_mesh = null
        else:
            if selected_mesh != null:
                selected_mesh.material_override = null
                selected_mesh = null

The error is happening on the line

selected_mesh.material_override.albedo_texture = selected_mesh.get_surface_material(result.shape).get_texture("albedo_texture")

To me, it does not look like the selected_mesh is being set to a null instance. Could someone explain what is causing this?

Upvotes: 0

Views: 546

Answers (1)

Theraot
Theraot

Reputation: 40295

Review the facts:

  • You are getting the error

    attempt to call function 'get_texture' in base 'null instance' on a null instance

    which tells you that you are trying to call get_texture in an invalid instance.

  • An you are getting in the line:

    selected_mesh.material_override.albedo_texture = selected_mesh.get_surface_material(result.shape).get_texture("albedo_texture")
    

Pop quiz! what is the thing that is an invalid instance?

Well, you are trying to call get_texture (which is what the error says) on this:

selected_mesh.get_surface_material(result.shape)

So that must be an invalid instance.


You might wonder if selected_mesh could be null. Well, if selected_mesh was null, then the error from Godot would say get_surface_material. Makes sense?

Well, actually if selected_mesh was null, you would have gotten the error when trying to set selected_mesh.material_override.


You seem to be making a wrong assumption. It is not a material per shape. I repeat: It is not a material per shape. Furthermore, these are the collision shapes, which are a physics concept. And the materials are associated with the surfaces of a mesh, which is a graphics concept.

You need to rethink your approach.

Upvotes: 0

Related Questions