Unity3d 在一个游戏对象数组中循环,单击c杀死上一个对象
我有一个包含游戏对象的数组,我想在单击时实例化第一个,如果我再次单击,杀死当前游戏对象并实例化数组中的下一个游戏对象Unity3d 在一个游戏对象数组中循环,单击c杀死上一个对象,unity3d,game-development,Unity3d,Game Development,我有一个包含游戏对象的数组,我想在单击时实例化第一个,如果我再次单击,杀死当前游戏对象并实例化数组中的下一个游戏对象 public Transform[] Cubes; Transform Clone; public Transform Spawnup; void Start() { Clone = Instantiate(Cubes[Random.Range(0, 3)], transform.position, transform.rotation) as Transform; Clon
public Transform[] Cubes;
Transform Clone;
public Transform Spawnup;
void Start()
{
Clone = Instantiate(Cubes[Random.Range(0, 3)], transform.position,
transform.rotation) as Transform;
Clone.parent = transform;
transform.position = Spawnup.position;
}
void FixedUpdate()
{
if (transform.childCount < 1)
{
transform.position = Spawnup.position;
Clone = Instantiate(Cubes[Random.Range(0, 3)], transform.position,
transform.rotation) as Transform;
Clone.parent = transform;
}
}
private public void ChangeOnClick()
{
//Destroy(gameObject);
//for (int i = 0; i < Cubes.Length; i++)
// {
// Clone = Instantiate(Cubes[0], Clone.position, Clone.rotation) as
Transform;
// }
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision && transform.childCount > 0)
{
Clone.parent = Spawnup;
}
}
}
我尝试使用int和++来实例化游戏对象,但每次单击时,所有游戏对象都会立即实例化。FixedUpdate函数中的代码应该在ChangeOnClick函数中 代码:
如果有帮助,请告诉我 谢谢你的回复。好的,我添加了If语句,检查子计数是否小于1。但是如何实例化数组中的下一个游戏对象?如果我再次点击下一个游戏对象?在我的阵列中,我有4个游戏对象,我希望通过0,1,2,3…0,1,2,3。。。每次单击时,我都会在编辑的答案中添加一个粗略的psudo代码。你可以试试看。此外,当索引达到array.length-1时,可以将其重置为0。
GameObject _PreviousClone;
int cubeIndex;
private public void ChangeOnClick()
{
if (transform.childCount < 1)
{
transform.position = Spawnup.position;
if(_PreviousClone && _PreviousClone.canBeDestroyed)
{
Destroy(_PreviousClone.gameobject);
cubeIndex++;
Clone = Instantiate(Cubes[cubeIndex], transform.position,
transform.rotation) as Transform;
[yourClass] _cloneClass = Clone.getcomponent<[Yourclass]>();
_cloneClass.canBeDestroyed = true;
_PreviousClone = Clone;
Clone.parent = transform;
if(cubeIndex==Cubes.length-1)
cubeIndex=0;
}
}
}