How to modify the Material of a Mesh Node from script?

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

I’d like to change colors of objects in my game from within a script.

How can I do this from GDscript?

:bust_in_silhouette: Reply From: SIsilicon

You can change the color of the mesh by changing the color of the material(which I assume is SpatialMaterial and is in the material_override property). So to change the material’s color, you run this code.

"Your MeshInstance".material_override.albedo_color = "Your new color"

But here is a thing to note. If multiple mesh instances share the same mesh/material then all of them will change if you try and change said mesh/material. So if you have, for example, two mesh instances with the same material, changing the material’s properties will change for both of them. So to change the individual color of each object, they will each need to have a unique material.

How would you access the material on the mesh vs the material on the object?

I’m trying to change the emission_energy on my material on the mesh but can’t seem to find the path to it.

Brinux | 2019-08-10 20:48

Meshes may have more than one material and are accessed with mesh.surface_get_material(int surface_idx).
Mesh Instances also have methods for setting and getting the materials for their corresponding mesh. get_surface_material(int surface_idx), set_surface_material(int surface_idx), and get_surface_material_count().

SIsilicon | 2019-08-15 15:55