如何通过标签找到 Light 组件?
How can i find the Light component by tag?
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;
}
}
}
}
因为我在层次结构中有其他 Light 组件,所以我向手电筒对象添加了一个标签,称为 Flashlight。
但是我怎样才能通过手电筒标签获得正确的灯光?
void Start ()
{
flashlight = FindGameObjectsWithTag("FlashLight").GetComponent<Light>();
}
引用自here
您可以创建一个 public 字段或 getter,您可以在其中选择准确的引用
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;
}
}
}
}
因为我在层次结构中有其他 Light 组件,所以我向手电筒对象添加了一个标签,称为 Flashlight。
但是我怎样才能通过手电筒标签获得正确的灯光?
void Start ()
{
flashlight = FindGameObjectsWithTag("FlashLight").GetComponent<Light>();
}
引用自here
您可以创建一个 public 字段或 getter,您可以在其中选择准确的引用