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
keypressed(KEYTAB):
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! :)

asked Sep 7, 2019 in Engine by LucaFox (17 points)

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

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

Sorry!

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.

answered Sep 7, 2019 by Zylann (21,719 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.