using System.Collections; using System.Collections.Generic; using UnityEngine; public class Example : MonoBehaviour { public GameObject[] lights; //存放燈光的陣列 void Start() { lights = GameObject.FindGameObjectsWithTag("Light"); //抓取場景上的燈光物件 for (int i = 0; i < lights.Length; i++) { lights[i].SetActive(false); //先關閉全部的燈光物件 } } void Update() { if (Input.GetKeyDown(KeyCode.Keypad1)) { for(int i = 0; i < lights.Length; i++) //使用迴圈抓取全部燈光 { lights[i].SetActive(true); //按下 1 開啟全部燈光 } } if (Input.GetKeyDown(KeyCode.Keypad0)) { for (int i = 0; i < lights.Length; i++) //使用迴圈抓取全部燈光 { lights[i].SetActive(false); //按下 0 關閉全部燈光 } } } }