如何在单击按钮时实例化数组中的下一个预制件?

How to instantiate next prefab in array on click on button?

我想在数组中实例化新的预制件,并在用户单击下一个按钮或上一个按钮时删除旧的预制件。

我有这样的游戏对象预制数组:

public GameObject[] prefabMoles = new GameObject[3];
 GameObject[] moles = new GameObject[3];
 
 void Start () {
 
     for(int i = 0; i < prefabMoles.Length; i++)
     {
         moles[i] = (GameObject) GameObject.Instantiate(prefabMoles[i], new Vector3((i-1)*5, 0, 0), Quaternion.identity);
     }
 
 }

所以现在我想在 canvas 上添加按钮,它将成为下一个字符,而前一个将 return 返回数组中的一个数字。

例如: 我有 Moles 数组并实例化 moles[2]; 单击下一步按钮,我想销毁痣 [2] 并实例化痣 [3]。

当我们在 moles[3] 上并且单击上一步时,我们必须销毁 moles[3] 并实例化 moles[2]。

产生新的效果会很好,但没关系。 然后当实例化新按钮时,按钮必须弹出以解锁或 select 如果角色已解锁。

如果可能的话,非常感谢社区的帮助。

如果你想做的是让你的按钮创建一个新按钮并销毁旧按钮,你可以执行这个可以添加到按钮操作的功能。

它看起来像:

int currentIndex = 0;
GameObject currentObject;

void Start()
{
     //Instantiate initial object
    currentObject = Instantiate(Moles[currentIndex]);
}

public void Previous()
{
    Destroy(currentObject);
    currentIndex --;
    currentIndex = currentIndex < 0 ? Moles.Length : currentIndex;
    currentObject = Instantiate(Moles[currentIndex]);
    SpawnEffects();
}    

public void Next()
{
    Destroy(currentObject);
    currentIndex ++;
    currentIndex = currentIndex > Moles.Length ? 0 : currentIndex;
    currentObject = Instantiate(Moles[currentIndex]);
    SpawnEffects();
}

void SpawnEffects()
{
    //put your desired Effects
}

本质上,您正在跟踪已创建的内容以及您要制作的下一个项目。