0 votes

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 = [
    preload("res://Assets/MapTiles/empty/Center.material"),
    preload("res://Assets/MapTiles/blue/Center.material"),
    preload("res://Assets/MapTiles/green/Center.material")
    ]


onready var center = $Circle


func _ready():
    pass


func changeCenter(newCenter):
    center.material/0 = centerMaterials[newCenter]
Godot version 3.2.3
in Engine by (244 points)

1 Answer

+1 vote
Best answer

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]).

by (3,144 points)
selected by

The first solution worked. Thanks!

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.