2017-04-27 14 views
0
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的标签。我如何通过标签找到Light组件?

但是我如何才能通过手电筒标签获得正确的光?

回答

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

here

+0

我所做的:手电筒= GameObject.FindGameObjectWithTag( “手电筒”);但得到错误: 错误不能隐式地将类型'UnityEngine.GameObject'转换为'UnityEngine.Light' –

+1

再次检查我的答案:) – Hristo

+1

非常感谢:) –

0

引用您可以创建一个公共领域或一个getter,当你随意,准确的参考