【发布时间】:2018-12-28 17:08:11
【问题描述】:
我最近刚接触 Unity 3D,目前正在处理我自己的第一个项目。对于我正在制作的游戏,我需要一个 spawner 功能,一旦敌人从平台上掉下来,它就会重新生成敌人的克隆。这是我现在的代码:
using UnityEngine;
public class spawner : MonoBehaviour
{
public GameObject enemyPrefab;
public float spawnHeight = 0.75f;
// Start is called before the first frame update
void Start()
{
spawnEnemy();
}
// Update is called once per frame
void Update()
{
if (enemyClone.transform.position.y < -10)
{
Destroy(enemyClone);
spawnEnemy();
}
}
public void spawnEnemy()
{
var enemyPosition = new Vector3(Random.Range(-5, 5), spawnHeight, Random.Range(-5, 5));
var enemyClone = Instantiate(enemyPrefab, enemyPosition, Quaternion.identity);
}
}
函数 spawnEnemy 本身工作正常,因为它在游戏开始时创建一个敌人,但不会产生更多的敌人。我收到消息:“Assets\spawner.cs(21,21): error CS0103: The name 'enemyClone' does not exist in the current context”。
我明白为什么我会收到这条消息,但是不知道如何让enemyClone 全球可用。
提前感谢大家,
苯乙腈
【问题讨论】: