C# 关于统一之光的问题
我试图在Unity中制作一个驾驶游戏,作为游戏的一部分,我试图让交通灯闪烁 这是我的剧本,贴在红绿灯上。交通灯的层次结构是: 交通灯A(基本交通灯)C# 关于统一之光的问题,c#,unity3d,light,C#,Unity3d,Light,我试图在Unity中制作一个驾驶游戏,作为游戏的一部分,我试图让交通灯闪烁 这是我的剧本,贴在红绿灯上。交通灯的层次结构是: 交通灯A(基本交通灯) 红灯(我正试图使之闪烁的灯) 我能做些什么来解决这个问题?(我对C#有点陌生)根据您的层次结构,您正试图从名为“红灯”的游戏对象(TrafficLight\u a”游戏对象的子对象)中访问灯光组件。为此,使用将“TrafficLight\u A/RedLight”传递给游戏对象。Find函数找到红光游戏对象,然后GetComponent()检索
- 红灯(我正试图使之闪烁的灯)
我能做些什么来解决这个问题?(我对
C#
有点陌生)根据您的层次结构,您正试图从名为“红灯”的游戏对象(TrafficLight\u a”游戏对象的子对象)中访问灯光组件。为此,使用将“TrafficLight\u A/RedLight”传递给游戏对象。Find
函数找到红光游戏对象,然后GetComponent()
检索灯光组件。您可以在唤醒
或启动
功能中执行此操作
无论何时需要查找子对象,都会像在文件路径中一样使用“/”
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;
}
public float totalSeconds=2;//闪光持续的总秒数
公共浮点最大强度=8;//闪光灯将达到的最大强度
公共光myLight;
无效唤醒()
{
//找到红灯
GameObject redlight=GameObject.Find(“交通灯/红灯”);
//将灯光组件连接到它
myLight=redlight.GetComponent();
}
公共IEnumerator flashNow()
{
浮动等待时间=总秒数/2;
//获得一半秒(一半变亮,一半变暗)
而(myLight.intensity0)
{
myLight.intensity-=Time.deltaTime/waitTime;//降低强度
收益返回空;
}
收益返回空;
}
Blink.cs代码在哪里?或者你贴的是Blink.cs?如果是这样,您可能希望将其更改为Flash.cs或将类的名称从Flash更改为Blink。不管怎样,您确定场景中存在红灯吗?为确保正确设置,请改为在检查器中设置它。@slaphshot33324如错误消息所示,它位于/Blink\u Script/Blink.cs
中。它作为脚本组件附加到TrafficLight\u A
。(上面的代码是Blink.cs
,但是当我将类定义更改为Blink
,我会得到'Assets/Blink\u Script/Blink.cs(4,15):错误CS0101:命名空间“global::”已包含“Blink”的定义。请参见编辑,但我的意思是请发布Blink.cs代码,或者如果发布的是Blink.cs,则该类也应称为Blink。@slaphshot33324我发布了Blink.cs
。我将类名和脚本名都更改为Blink\u Light
,并且错误仍然存在。我的评论不是为了解决问题,只是为了指导您进行良好的编程实践。作为最佳实践,.cs文件的名称应始终与您的类名相同。它修复了错误,并且可以找到红灯
!但由于某些原因,它不会闪烁:(你的参考问题解决了。你可以为你的闪光问题创建新问题。我相信很多像德胡戈这样的人可以帮助你。)
Assets/Blink_Script/Blink_Light.cs: error CS1525: Unexpected symbol "RedLight"
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;
}