How to change objects color and opacity without messing with the materials?

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

The problem is as follows:

I have a few objects that are using the same material. I want each to have different color (texture modulated by an RGB value) and different opacity.

There’s a “Modulate color” value in a Sprite3D node that allows to make per-object changes regardless of the material, but this doesn’t work if I use a non-standard material.

TL;DR;
How do I modify objects’ color and alpha without changing the look of all other objects using the same material?

:bust_in_silhouette: Reply From: rgrams

See the answers to my question here: How do I make Material/Shader Instances (2D) - Archive - Godot Forum (assuming the same thing works for 3D)

Link offline.

H4kor | 2016-09-25 13:24

Thanks, fixed.

rgrams | 2016-09-25 15:04

Looks like it doesn’t work for 3D…

extends MeshInstance

func _ready():
	var mat = get_material().duplicate(true)
	set_material(mat)

This fails with the following error:

Invalid call. Nonexistent function 'get_material' in base 'MeshInstance (Flare.gd)'.

Adding mesh fixes this issue:

extends MeshInstance

func _ready():
	var mat = mesh.get_material().duplicate(true)
	mesh.set_material(mat)

unfa | 2019-02-14 23:18