How do I change color of a rigidbody, etc. programmatically)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ingo
:warning: Old Version Published before Godot 3 was released.

I have started from a Script from the examples, from mousepick.scn .
Below is what I have tried after serching, but both doesn’t work.
Any ideas?

extends RigidBody

# Member variables
var gray_mat = FixedMaterial.new()
var selected = false


func _input_event(camera, event, pos, normal, shape):
	# Doesn't work
	#gray_mat.set_parameter(Material.PARAM_DIFFUSE, Color(0.0,1.0,0.0))
	if (event.type==InputEvent.MOUSE_BUTTON and event.pressed):
		if (not selected):
			get_node("mesh").set_material_override(gray_mat)
		else:
			get_node("mesh").set_material_override(null)
		
		selected = not selected


func _mouse_enter():
	get_node("mesh").set_scale(Vector3(1.1, 1.1, 1.1))
	
	# Doesn't work
	get_node("mesh").surface_get_material(0).set_parameter(Material.PARAM_DIFFUSE, Color(0,1,0))


func _mouse_exit():
	get_node("mesh").set_scale(Vector3(1, 1, 1))

Thank you.

:bust_in_silhouette: Reply From: GameVisitor

I just tried this and it worked :

get_node(“mesh”).surface_get_material(0).set_parameter(0, Color(0,1,0))

Probably not the cleanest solution but a quick workaround, since Godot was complaining about an incorrect index :

Invalid get index ‘PARAM_DIFFUSE’ (on base: ‘GDNativeClass’).

Wrap your code in backticks to format correctly:

getnode("mesh").surface_get_material(0).set_parameter(0, Color(0,1,0))

kidscancode | 2017-07-30 23:07

For the exampleproject, there was a “get_mesh()” missing.

getnode("mesh").get_mesh().surface_get_material(0).set_parameter(0, Color(0,1,0))

Not sure, why the debugger didn’t show me any errors, after restarting, I saw the errors, too.

ingo | 2017-07-31 04:00