(スクリプト)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ListBlock : MonoBehaviour
{
public GameObject blockPrefab;
private List<Gameobject> blockList = new List<Gameobject>();
void Start()
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
for (int k = 0; k < 10; k++)
{
GameObject block = Instantiate(blockPrefab, new Vector3(-4.5f + i, 0.5f + j, -4.5f + k), Quaternion.identity);
blockList.Add(block);
}
}
}
print(blockList.Count);
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.Space))
{
for (int i = 0; i < 20; i++)
{
var num = Random.Range(0, blockList.Count);
print("ナンバー" + num);
Destroy(blockList[num]);
blockList.RemoveAt(num);
}
print(blockList.Count);
}
}
}
(実行結果)
- スペースキーを押すごとに、ランダムに選んだ20個のCubeを削除する。