- Attach the script below to the dropdown object
- Drag and drop the directional light onto into the Directional Light Object slot in the inspector
- Also in the dropdown object inspector, click the plus button in the On Value Changed field.
- Drag and drop the dropdown object itself into the field beneath Runtime Only selector.
- Click No Function selector and change to the SetDropdownColor > UpdateColor.
–
The script will look for the light component attached to the directional light, then whenever the dropdown is changed, it will send the integer ID of the dropdown to that function in the script, which is a simple switch statement based on that integer ID, setting the color of the light.
–
Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class SetDropdownColor : Monobehaviour {
public GameObject directionalLightObject;
private Light directionalLight;
void Start() {
directionalLight = directionalLightObject.GetComponent<Light>();
}
public void UpdateColor (int dropdownValue) {
if(directionalLight){
switch(dropdownValue){
case 0: // green
directionalLight.color = Color.green;
break;
case 1: // red
directionalLight.color = Color.red;
break;
case 2: // blue
directionalLight.color = Color.blue;
break;
case 3: // yellow
directionalLight.color = Color.yellow;
break;
}
}
}
}