在 Unity 中引用灯光的问题
Issues referencing a light in Unity
我正在尝试在 Unity 中制作一款驾驶游戏,作为其中的一部分,我正在尝试让交通灯闪烁。
这是我的脚本,我附在了红绿灯上。红绿灯的层级为:
TrafficLight_A (the base traffic light)
- RedLight (the light I'm trying to make flash)
using UnityEngine;
using System.Collections;
public class Blink_Light : MonoBehaviour
{
public float totalSeconds = 2; // The total of seconds the flash wil last
public float maxIntensity = 8; // The maximum intensity the flash will reach
public Light myLight = Light RedLight; // The light (error)
public IEnumerator flashNow ()
{
float waitTime = totalSeconds / 2;
// Get half of the seconds (One half to get brighter and one to get darker)
while (myLight.intensity < maxIntensity) {
myLight.intensity += Time.deltaTime / waitTime; // Increase intensity
yield return null;
}
while (myLight.intensity > 0) {
myLight.intensity -= Time.deltaTime / waitTime; //Decrease intensity
yield return null;
}
yield return null;
}
}
但是,我得到一个错误:
Assets/Blink_Script/Blink_Light.cs: error CS1525: Unexpected symbol "RedLight"
我该怎么做才能解决这个问题? (我对 C#
有点陌生)
根据您的层次结构,您正在尝试从名为 "RedLight" 的游戏对象访问 Light
组件,该游戏对象是 "TrafficLight_A" 游戏对象的子对象。为此,请将 "TrafficLight_A/RedLight" 传递给 GameObject.Find
函数,该函数会找到 RedLight 游戏对象,然后 GetComponent<Light>()
检索 Light
组件。您可以在 Awake
或 Start
函数中执行此操作。
每当您需要查找子对象时,就像在文件路径中一样使用“/”。
public float totalSeconds = 2; // The total of seconds the flash wil last
public float maxIntensity = 8; // The maximum intensity the flash will reach
public Light myLight;
void Awake()
{
//Find the RedLight
GameObject redlight = GameObject.Find("TrafficLight_A/RedLight");
//Get the Light component attached to it
myLight = redlight.GetComponent<Light>();
}
public IEnumerator flashNow()
{
float waitTime = totalSeconds / 2;
// Get half of the seconds (One half to get brighter and one to get darker)
while (myLight.intensity < maxIntensity)
{
myLight.intensity += Time.deltaTime / waitTime; // Increase intensity
yield return null;
}
while (myLight.intensity > 0)
{
myLight.intensity -= Time.deltaTime / waitTime; //Decrease intensity
yield return null;
}
yield return null;
}
我正在尝试在 Unity 中制作一款驾驶游戏,作为其中的一部分,我正在尝试让交通灯闪烁。
这是我的脚本,我附在了红绿灯上。红绿灯的层级为:
TrafficLight_A (the base traffic light)
- RedLight (the light I'm trying to make flash)
using UnityEngine;
using System.Collections;
public class Blink_Light : MonoBehaviour
{
public float totalSeconds = 2; // The total of seconds the flash wil last
public float maxIntensity = 8; // The maximum intensity the flash will reach
public Light myLight = Light RedLight; // The light (error)
public IEnumerator flashNow ()
{
float waitTime = totalSeconds / 2;
// Get half of the seconds (One half to get brighter and one to get darker)
while (myLight.intensity < maxIntensity) {
myLight.intensity += Time.deltaTime / waitTime; // Increase intensity
yield return null;
}
while (myLight.intensity > 0) {
myLight.intensity -= Time.deltaTime / waitTime; //Decrease intensity
yield return null;
}
yield return null;
}
}
但是,我得到一个错误:
Assets/Blink_Script/Blink_Light.cs: error CS1525: Unexpected symbol "RedLight"
我该怎么做才能解决这个问题? (我对 C#
有点陌生)
根据您的层次结构,您正在尝试从名为 "RedLight" 的游戏对象访问 Light
组件,该游戏对象是 "TrafficLight_A" 游戏对象的子对象。为此,请将 "TrafficLight_A/RedLight" 传递给 GameObject.Find
函数,该函数会找到 RedLight 游戏对象,然后 GetComponent<Light>()
检索 Light
组件。您可以在 Awake
或 Start
函数中执行此操作。
每当您需要查找子对象时,就像在文件路径中一样使用“/”。
public float totalSeconds = 2; // The total of seconds the flash wil last
public float maxIntensity = 8; // The maximum intensity the flash will reach
public Light myLight;
void Awake()
{
//Find the RedLight
GameObject redlight = GameObject.Find("TrafficLight_A/RedLight");
//Get the Light component attached to it
myLight = redlight.GetComponent<Light>();
}
public IEnumerator flashNow()
{
float waitTime = totalSeconds / 2;
// Get half of the seconds (One half to get brighter and one to get darker)
while (myLight.intensity < maxIntensity)
{
myLight.intensity += Time.deltaTime / waitTime; // Increase intensity
yield return null;
}
while (myLight.intensity > 0)
{
myLight.intensity -= Time.deltaTime / waitTime; //Decrease intensity
yield return null;
}
yield return null;
}