简体   繁体   English

如何通过标签找到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;
            }
        }
    }
}

Since i have other Light components in the Hierarchy i added a tag to the flashlight object called it Flashlight. 由于我在层次结构中还有其他Light组件,因此我向手电筒对象添加了一个名为Flashlight的标签。

But how can i get the correct Light by the Flashlight tag ? 但是,如何通过Flashlight标签获取正确的光源?

void Start ()
{
    flashlight = FindGameObjectsWithTag("FlashLight").GetComponent<Light>();
}

Referenced from here 这里引用

您可以创建一个公共场所或一个吸气剂,从中选择确切的参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM