在我的场景中,我实例化了一个Brick,这个Brick具有将其实例化为敌人的脚本。
问题是实例化的Brick的脚本不起作用,所以敌人没有实例化,如何解决?
第一个脚本
public GameObject[] Bricks;
void SpawnBricks(int numCubes = 20, float startY = 3, float delta = 0.6f, float AngleDis = 3f)
{
int Rand = Random.Range(0, Bricks.Length);
for (int i = 0; i < numCubes; ++i)
{
var Brick = Instantiate(Bricks[Rand], new Vector3(0, startY - (float)i * delta, 0), Quaternion.identity);
Brick.transform.parent = gameObject.transform;
}
}
砖墙脚本:
public GameObject[] Enemies;
public void Awake()
{
SpawnTheEnemies();
}
public void SpawnTheEnemies()
{
int Rand = Random.Range(0, Enemies.Length);
var Buildings = Instantiate(Enemies[Rand], transform.position, Quaternion.Euler(0, Random.Range(0, 360), 0));
Buildings.transform.parent = gameObject.transform;
}
答案 0 :(得分:0)
您的问题在唤醒功能内部。当生成Brick时,它在Enemies数组内不包含任何元素。 在使用SpawnTheEnemies函数之前,您可能需要在数组内分配敌人引用。
public GameObject[] Enemies;
public void Awake()
{
Enemies = FindObjectsOfType<[YourEnemyScriptName]>(); // Insert your Enemies script name inside [].
}
void Start(){
SpawnTheEnemies();
}
public void SpawnTheEnemies()
{
int Rand = Random.Range(0, Enemies.Length);
GameObject Buildings = Instantiate(Enemies[Rand], transform.position, Quaternion.Euler(0, Random.Range(0, 360), 0));
Buildings.transform.parent = gameObject.transform;
}
让我知道是否有帮助。