Change texture via script

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

I’m trying to change a texture on a key press. The cube has texture 1 (blue) normally whilst the down key is pressed the cube uses texture 2 (red). Once the key is released the cube goes back to the first texture is is blue once again.

I’m halfway there

extends Spatial

onready var cubey = get_node("/root/world/cube")

onready var mat1 = preload("res://icon.png")
onready var mat2 = preload("res://icon_inv.png")

func _input(event):
	if event.is_action_pressed("down"):
		cubey.set_surface_material(0, mat2)
    # else:
 	#      cubey.set_surface_material(0, mat1) # not working

Two things. I’m swapping the material, not the texture. And secondly the cube should revert to the first texture after the key is up.

:bust_in_silhouette: Reply From: BlotoPK

when you do cubey.set_surface_material(0, mat2), you’re trying to change the MATERIAL, not the texture (albedo) of the current material. You have to do something like: cubey.get_surface_material(0).set(“albedo_texture”, mat2), because mat2 is a texture, not a material.

It was


with the all important underscores, but that’s what I needed. Thanks!

func _input(event):
    if event.is_action_pressed("down"):
    	cubey.get_surface_material(0).set("albedo_texture", mat1)
    	cubey.get_surface_material(0).set("albedo_texture", mat0

ghoulfool | 2023-02-07 08:51