How to modify the Material of a CSGMesh Node from script?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By manmaor

I’d like to change material of CSGMesh in my game from within a script.

How can I do this from GDscript?

I have tscn with root of CSGMesh, and the Mesh property is a tres file.
I spawn this tscn multiple time and when I click on one of the tscn I want it to change material to premade Material and delete it when the click is released.
In the past I used Area(with MeshInstance named Mesh) for this and changed the material via:

extends Area
...
$Mesh.material_override = load("res://materials/lowpoly_half.material")

but the CSGMesh didn’t seem to have the material_override property,
and if I’m using this:

extends CSGMesh
...
mesh.surface_set_material(1, load("res://materials/lowpoly_half.material"))

every instance of the tscn change it’s material.