• 3D
  • Is there a way to change what material an object is using, using a script

Hello

I am working on a 3D game and I was hoping to add a mechanic where the character would change color at the press of a button. I am using GD Script and I can not find a way to change the material of an object mid game. Is there a way to accomplish this

Thanks, Ace : )

You can get the material if you call this function on a mesh instance, you can do this on _ready() (make sure to define "var mat" at the top of the script so the rest of the functions can see it):

mat = get_surface_material(0)

Note that if your script is not directly on the mesh instance, you will need to use get_node("") to get it.

Then to change the material properties you can use this to change the color to something random:

mat.albedo_color = Color(randf(), randf(), randf())

Hope that helps.

Cool. Those are just red/green/blue values from 0.0 to 1.0.

For example, pure red would be:

Color(1.0, 0.0, 0.0)

Or white would be:

Color(1.0, 1.0, 1.0)

awesome. One last thing. You mentioned having to use get_node("") if the script is not directly on the mesh, but I don't understand how get_node works. My mesh is a child of a kinimatic body which is where my script is. Does the get_node function allow me to use get_surface_material

get_node() uses a format similar to navigating on the command-line in a console. If you wanted to get a direct child of the current node you would pass in the node's name like:

get_node("MyMesh")

Where "MyMesh" is the name of your mesh instance.

Of if there were 2 levels (for example, the script was on a higher node) you can do this:

get_node("MyBody/MyMesh")

See here for more details: https://docs.godotengine.org/en/3.1/classes/class_node.html#class-node-method-get-node

func _ready():
	get_node("Player Mesh")
	mat = get_surface_material(0)

So I got the mesh node, but it still gives me this error

Method 'get_surface_material' is not declared in the current class.

Am I missing somthing get get_surface_material into the class?

get_surface_material is a member function. Which means you have to call it on an instance of the class.

For example, something like this would work (assuming "Player Mesh" is a mesh instance):

func _ready():
    var player_node = get_node("Player Mesh")
    mat = player_node.get_surface_material(0)

Ok, so I have to spesify the mesh mode when using get_surface_material. Got It

Thank you for your help : )

a year later

Hello, im just trying to set other material how can i replace the material/0 to other material already created in the editor?

@T3chn0 said: Hello, im just trying to set other material how can i replace the material/0 to other material already created in the editor?

If you are trying to set a material, then the set_surface_material function is what you'll need to use. Something like this for example:

extends MeshInstance

# set this in the Godot editor! You can find it in the inspector
# after assigning this script and then selecting the node
export (Material) var new_material

func _ready():
	set_surface_material(0, new_material)