0 votes

I want to change the base color of my mesh when I press TAB.

The script:

var color1 = Color(0.3,0.3,0.3,1)

func input(event):
if Input.is
color1 = getnode("InterfaceRoot/TabCColors/CPMesh").getpick_color()

The problem:

The MeshInstance's mesh can be changed in my game. If I change mesh and then press TAB to change the color, it basically says the material no longer exists. I suppose this happens because when you change the mesh, you lose the material applied to it. Problem is, I can't successfully create a new material, not even copy-pasting from tutorials on the web. Also, results are variable. For example, if I don't change mesh and material I set by hand to be the default (a cube with a basic material) it works smoothly.

I'm an absolute beginner of Godot and GDScript so please try to be basic in your answer! Thanks to everyone! :)

in Engine by (17 points)

PLEASE NOTE: the code I pasted above misses this foundamental line:

getnode("...").mesh.surfacegetmaterial(0).albedocolor = color1


1 Answer

0 votes

There are two ways you could go:

Solution 1: when you change the color, remember which color it is. Then when you change the mesh, change the color again on the new mesh.

Solution 2: use material_override instead of having the material inside your mesh. This way you can change the mesh but the override material will remain because it's on the MeshInstance.

by (27,603 points)
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.
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 webmaster@godotengine.org with your username.