Error access and change shader parameters with gdscript

Godot Version

v4.3.dev5.official [89f70e98d]


I tried to access and change the shader parameter Color for a basic material with a simple shader but I got this error for access, any idea ?
This is the gdscript to the main Node3D :

extends Node3D
var shader : Shader
var material : ShaderMaterial
@onready var meshInstance: MeshInstance3D = $MeshInstance3D_001

func _ready():

	if meshInstance.material is ShaderMaterial:
		material = meshInstance.material
		shader = material.shader

		# Afișează valoarea parametrului 'Color'
		var color_param = shader.get_param_list().find("Color")
		if color_param != -1:
			var current_color = material.get_shader_param(color_param)
			print("Valoarea curentă a parametrului 'Color':", current_color)

			# Modifică valoarea parametrului 'Color' cu o nouă valoare
			var new_color = Color(1, 0, 0, 1)  # Culoare roșie
			material.set_shader_param(color_param, new_color)
			print("Noua valoare a parametrului 'Color':", new_color)
			print("Parametrul 'Color' nu a fost găsit în shaderul test.gdshader")

The error screenshot :

As the error is saying, you’re trying to access a property that doesn’t exist in MeshInstance3D, the material you’re trying to access is inside the mesh property so you need to reference to her before for access the material: meshInstance.mesh.material

Alternatively you can use meshInstance.get_active_material(0) to get any material inside your meshInstance

MeshInstance3D — Godot Engine (stable) documentation in English

1 Like