The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

+4 votes

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

How can I do this from GDscript?

in Engine by (217 points)

1 Answer

+4 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.

by (3,938 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 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.