using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.ThirdPerson;
public class Flashlight : MonoBehaviour {
Light flashlight;
// Use this for initialization
void Start ()
{
flashlight = GetComponent<Light>();
}
// Update is called once per frame
void Update ()
{
if (Input.GetKeyDown(KeyCode.F))
{
if (flashlight.enabled)
{
flashlight.enabled = false;
}
else
{
flashlight.enabled = true;
}
}
}
}
由于我在Hierarchy中有其他Light组件,我在手电筒对象中添加了一个名为Flashlight的标签。
但是如何通过Flashlight标签获得正确的Light?
答案 0 :(得分:2)
void Start ()
{
flashlight = FindGameObjectsWithTag("FlashLight").GetComponent<Light>();
}
引自here
答案 1 :(得分:0)
您可以创建公共字段或getter,从中选择准确的引用