C# 如何通过标记找到灯光组件? 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 使用UnityStandardAssets.Characters.ThirdPerson; 公共级手电筒:单一行为{ 手电筒; //用于初始化 无效开始() { flashlight=GetComponent(); } //每帧调用一次更新 无效更新() { if(Input.GetKeyDown(KeyCode.F)) { 如果(手电筒。已启用) { flashlight.enabled=false; } 其他的 { flashlight.enabled=true; } } } }
因为我在层次结构中有其他灯光组件,所以我向闪光灯对象添加了一个名为flashlight的标记 但是如何通过手电筒标签获得正确的灯光呢?C# 如何通过标记找到灯光组件? 使用系统集合; 使用System.Collections.Generic; 使用UnityEngine; 使用UnityStandardAssets.Characters.ThirdPerson; 公共级手电筒:单一行为{ 手电筒; //用于初始化 无效开始() { flashlight=GetComponent(); } //每帧调用一次更新 无效更新() { if(Input.GetKeyDown(KeyCode.F)) { 如果(手电筒。已启用) { flashlight.enabled=false; } 其他的 { flashlight.enabled=true; } } } },c#,unity3d,unity5,C#,Unity3d,Unity5,因为我在层次结构中有其他灯光组件,所以我向闪光灯对象添加了一个名为flashlight的标记 但是如何通过手电筒标签获得正确的灯光呢?void Start() using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityStandardAssets.Characters.ThirdPerson; public class Flashlight : MonoBehaviour
void Start()
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;
}
}
}
}
{
flashlight=FindGameObjectsWithTag(“flashlight”).GetComponent();
}
引用自无效开始()
{
flashlight=FindGameObjectsWithTag(“flashlight”).GetComponent();
}
引用自您可以创建一个公共字段或一个getter,您可以在其中选择准确的引用您可以创建一个公共字段或一个getter,您可以在其中选择准确的引用I did:flashlight=GameObject.FindGameObjectWithTag(“flashlight”);但获取错误:错误无法将类型“UnityEngine.GameObject”隐式转换为“UnityEngine.Light”,请再次检查我的答案:)我做到了:flashlight=GameObject.FindGameObjectWithTag(“flashlight”);但获取错误:错误无法将类型“UnityEngine.GameObject”隐式转换为“UnityEngine.Light”再次检查我的答案:)
void Start ()
{
flashlight = FindGameObjectsWithTag("FlashLight").GetComponent<Light>();
}