unity3d - 当我使用这个脚本时,它会导致我的游戏崩溃。我究竟做错了什么?
问题描述
我想在事件触发后关灯几秒钟,但它不起作用。我对 C# 还是很陌生,所以如果代码很难看,我很抱歉 :(
[SerializeField] static public bool lightsOut;
private Light lightComponent;
void Start()
{
lightComponent = gameObject.GetComponent<Light>();
Time.timeScale = 0;
}
void Update()
{
if (DeleteAfterFix.isFixed1)
{
TurnOffLights();
}
}
IEnumerator WaitAndTurnOn()
{
yield return new WaitForSeconds(2);
TurnLightsOn();
}
void TurnOffLights()
{
Destroy(lightComponent);
WaitAndTurnOn();
}
void TurnLightsOn()
{
gameObject.AddComponent<Light>();
gameObject.GetComponent<Light>().type = 0;
gameObject.GetComponent<Light>().intensity = 1 / 3;
gameObject.GetComponent<Light>().range = 15 / 2;
gameObject.GetComponent<Light>().spotAngle = 165;
}
解决方案
呼吁
Time.timeScale = 0;
inStart()
函数停止游戏时间,即暂停游戏。删除它。
WaitAndTurnOn
应该作为协程调用:
StartCoroutine(WaitAndTurnOn());
Update
如果您使用协程,则不需要该功能。
void Start()
{
StartCoroutine(WaitAndTurnOn());
}
IEnumerator WaitAndTurnOn()
{
yield return new WaitForSeconds(2);
TurnLightsOn();
}
void TurnLightsOff()
{
Destroy(GetComponent<Light>());
StartCoroutine(WaitAndTurnOn());
}
void TurnLightsOn()
{
gameObject.AddComponent<Light>();
gameObject.GetComponent<Light>().type = 0;
gameObject.GetComponent<Light>().intensity = 1 / 3;
gameObject.GetComponent<Light>().range = 15 / 2;
gameObject.GetComponent<Light>().spotAngle = 165;
}
推荐阅读
- ios - 更新按钮标题
- python-2.7 - 为什么我得到一个无限循环?[Python 2.7]
- python - 程序一直在无限函数中循环?
- asp.net-mvc - Json 格式的 web api 的结果应该只返回特定的数据
- postgresql - 为什么postgres容器在Gitlab CI中忽略/docker-entrypoint-initdb.d/*
- python-2.7 - Python 2 - 除法乘以 10 每次返回 0
- excel - 如何在不将值转换为公式的情况下更改 Power Query 中的参数?
- arrays - 如何使用Java中文本文件中的元素对平均标记进行排序
- sql - 带有计数的数据透视表 - Oracle SQL
- java - ImageView 用相同的按钮交换图像