How to change mesh material using code?

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

Hello, I’m new to Godot
I have an scene (made with Blender) that has two materials. I want to change one of the materials using code. This code illustrates what I want, but doesn’t work (the variable center is the MeshInstance with the two materials):

extends Spatial

const centerMaterials : Array = [

onready var center = $Circle

func _ready():

func changeCenter(newCenter):
	center.material/0 = centerMaterials[newCenter]
:bust_in_silhouette: Reply From: Ertain

Have you tried code like this?

func changeCenter(newCenter):
    center.set_surface_material(0, centerMaterials[newCenter])

If not, also try the code center.get_mesh().set_surface_material(0, centerMaterials[newCenter]).

The first solution worked. Thanks!

abelgutierrez99 | 2020-12-29 22:17