+3 votes

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

How can I do this from GDscript?

asked Jun 1, 2018 in Engine by unfa (179 points)

1 Answer

+2 votes

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.

answered Jun 1, 2018 by SIsilicon (3,796 points)

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.

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

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 How to use this Q&A? before posting your first questions.