How to change the image/texture of a sprite when it enters a specific area

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ziggy

so basically i have a kinematicbody2d with an area2d, collisionshape2d, and sprite as children of the kinematic node, ive written some code that enables it to be dragged across the screen by the user, i want to make it so that the user when they drag the kinematic body over a specific area the texture of the of the sprite changes

extends Area2D

var new_texture = “res://Assets/Passport1Closed.png”

func _on_Booth2D_body_entered(body: PhysicsBody2D)-> void:
$PassSprite.set_texture(new_texture)

i have this so far and i know its a simple solution but cant find the answer!

for context these are some of the errors i get

Attempt to call function ‘set_texture’ in base ‘null instance’ on a null instance.

The argument ‘body’ is never used in the function ‘_on_Booth2D_body_entered’. If this is intended, prefix it with an underscore: ‘_body’

The argument 'viewport is never used in the function '_on_PassArea_input_event. If this is intended, prefix it with an underscore: '_viewport*

The argument ‘event’ is never used in the function '_on_PassArea_input_event. If this is intended, prefix it with an underscore: '_event

The argument 'shape_jdx is never used in the function '_on_PassArea_input_event. If this is intended, prefix it with an underscore: "shape_idx

The argument body’ is never used in the function 'on_Booth2D body_entered! If this is intended, prefix it with an underscore: ’ body

ziggy | 2022-12-14 17:12

So, there’s only one actual error in the above messages. That’s the first one where you’re apparently trying to call set_texture() on an invalid object reference.

Looking at the posted code, that means that your $PassSprite reference is null. Where is that node in the scene tree compared to the node that contains the above script? It’d need to be a direct child for that code to work as intended.

jgodfrey | 2022-12-14 17:35

this makes a lot of sense, so i went and added my sprite to make it a child of the node where the above script is attached but now i have this error :frowning:

Invalid type in function ‘set_texture’ in base ‘Sprite (PassSpriteScr.gd)’. Cannot convert argument 1 from String to Object.

ziggy | 2022-12-14 18:53

Ah, yeah, that’s because the texture property expects a Texture, not a String containing the name of the texture’s source file. You can fix it by preloading the resource:

So, instead of this:

var new_texture = "res://Assets/Passport1Closed.png"

You want this:

var new_texture = preload("res://Assets/Passport1Closed.png")

jgodfrey | 2022-12-14 18:59

it worked your a legend !!!

ziggy | 2022-12-14 19:01

Also, you can set that texture using the texture property directly (no need to use the property setter). So:

$PassSprite.texture = new_texture

jgodfrey | 2022-12-14 19:08

:bust_in_silhouette: Reply From: jgodfrey

Based on the above discussion, the solution was 2-fold…

  • Fix the invalid $PassSprite reference by pointing it to a valid Sprite node
  • Pass an actual Texture to the sprite’s texture property, rather than passing a string containing the name of the image file.