unity3d - 为什么我的 powerup 预制件可以工作,但实例化的克隆却不行?
问题描述
这是我的播放器脚本中的代码:
void OnTriggerEnter2D (Collider2D collider2D)
{
if (collider2D.gameObject.tag == "powerup")
{
PoweredUp = true;
PowerupTimer = PowerupTimer;
PowerupTimer -= Time.deltaTime;
}
}
...这是上电本身:
void OnTriggerEnter2D(Collider2D collider2D)
{
if (collider2D.gameObject.tag == "Player")
{
Destroy(gameObject);
}
}
出于某种原因,我直接放入场景中的任何预制件都可以正常工作,但是实例化的预制件会被破坏,但由于某种原因,不要触发“PoweredUp”布尔值。
这是我在 spawner 对象上的一个更复杂的脚本:
public GameObject powerup;
public float minWait;
public float maxWait;
private bool isSpawning;
IEnumerator SpawnObject(float seconds)
{
Debug.Log("Waiting for " + seconds + " seconds");
yield return new WaitForSeconds(seconds);
Instantiate(powerup, transform.position, transform.rotation);
isSpawning = false;
}
void Update()
{
if (!isSpawning)
{
isSpawning = true;
StartCoroutine(SpawnObject(UnityEngine.Random.Range(minWait, maxWait)));
}
有什么解决方案,或者我可以更有效地做到这一点的方法吗?
更新:我发现 Player 对象上的脚本运行良好,但布尔值仍然没有改变。
解决方案
好的,我明白了,有一个编译器警告引用了“PowerupTimer = PowerupTimer”这一行,所以我将其更改为“PowerupTimer = 7f”,这几乎是一回事。
推荐阅读
- google-cloud-dataflow - 使用 Python SDK 加入多个流
- python-3.x - 如何实现自定义排序算法以接受 python 中的任何类型?
- python - 如何使用通过熊猫从 csv 读取的值填充数组?
- angular - 如何将 HttpClient 获取请求值存储在二维数组中 - Angular 6
- git - Jenkins-Github 集成-即席错误-错误:克隆远程 repo 'origin' 时出错
- php - 我们可以在同一页面的另一种形式中使用来自一种形式的用户输入数据吗
- php - 为什么 execute() PDO 函数返回 false?
- react-native - object 对象:在 Formik 值中未定义
- javascript - 如何在 vue 即时搜索中创建自定义细化列表?
- python - 如何使用 Pscycopg2 将字典插入 Postgresql 表