World environment won't change in script?

Godot Version

4.6

I am trying to change the world environment background energy, but when I run the game and press the key I have it set to it gives me this error. Invalid assignment of property or key ‘background_energy_multiplier’ with value of type ‘int’ on a base object of type ‘WorldEnvironment’. This is probably something simple but I am still pretty new so if someone could help me out intime that would be great.

extends Node3D

@onready var world_environment: WorldEnvironment = $"../WorldEnvironment"
@onready var quad: MeshInstance3D = $"../Quad"

var light_stat
func _input(event):
	if event.is_action_pressed("Lights"):
		light_stat = 1
		world_environment.background_energy_multiplier = 0
		quad.emission_energy_multiplier = 16
	if event.is_action_pressed("Lights") and light_stat == 1:
		world_environment.background_energy_multiplier = 1
		quad.emission_energy_multiplier = 0
	else:
		pass

background_energy_multiplier property is not part of the WorldEnvironment node, but Environment resource, which you can access with environment property on your WorldEnvironment node.

This should work:

world_environment.environment.background_energy_multiplier = 0

I suggest you to check out the documentation of both.

1 Like