アイテムをランダムに出現させる
![10cee748 8432 47ce b7c3 50fa68c3c92f](https://codegenius.org/uploads/slide/image/2161/10cee748-8432-47ce-b7c3-50fa68c3c92f.jpeg)
![13384bde 89ab 4ad9 ae4d e858f07c50e4](https://codegenius.org/uploads/slide/image/2162/13384bde-89ab-4ad9-ae4d-e858f07c50e4.jpeg)
アイテムをランダムに発生させる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyObject : MonoBehaviour
{
public GameObject effectPrefab;
public GameObject effectPrefab2;
public int objectHP;
// ★改良
// 配列の定義・・・>複数のデータを入れることのできる箱の作成
// public GameObject itemPrefab; この一行をコメントアウトする
public GameObject[] itemPrefabs;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Shell"))
{
objectHP -= 1;
if (objectHP > 0)
{
Destroy(other.gameObject);
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 2.0f);
}
else
{
Destroy(other.gameObject);
GameObject effect2 = Instantiate(effectPrefab2, transform.position, Quaternion.identity);
Destroy(effect2, 2.0f);
Destroy(this.gameObject);
// ★改良
// ランダム機能を使う
// 「Length」というプロパティを使うと、配列の要素数(「幾つの」データが入っているか)を測ることができます。
GameObject dropItem = itemPrefabs[Random.Range(0, itemPrefabs.Length)];
Vector3 pos = transform.position;
// ★改良
// itemPrefabをdropItemに変更する。
Instantiate(dropItem, new Vector3(pos.x, pos.y + 0.5f, pos.z), Quaternion.identity);
}
}
}
}
![04061b40 e5a5 4c05 b7ed cb2378184801](https://codegenius.org/uploads/slide/image/2163/04061b40-e5a5-4c05-b7ed-cb2378184801.jpeg)
![D509218b 587a 4e03 872a 9188c70b21ec](https://codegenius.org/uploads/slide/image/2164/d509218b-587a-4e03-872a-9188c70b21ec.jpeg)
![00dc328a 8548 4bae 8452 09fa4e8f3bbe](https://codegenius.org/uploads/slide/image/2165/00dc328a-8548-4bae-8452-09fa4e8f3bbe.jpeg)
BattleTank(基礎/全31回)
他のコースを見る![10cee748 8432 47ce b7c3 50fa68c3c92f](https://codegenius.org/uploads/slide/image/2161/10cee748-8432-47ce-b7c3-50fa68c3c92f.jpeg)
![13384bde 89ab 4ad9 ae4d e858f07c50e4](https://codegenius.org/uploads/slide/image/2162/13384bde-89ab-4ad9-ae4d-e858f07c50e4.jpeg)
アイテムをランダムに発生させる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyObject : MonoBehaviour
{
public GameObject effectPrefab;
public GameObject effectPrefab2;
public int objectHP;
// ★改良
// 配列の定義・・・>複数のデータを入れることのできる箱の作成
// public GameObject itemPrefab; この一行をコメントアウトする
public GameObject[] itemPrefabs;
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Shell"))
{
objectHP -= 1;
if (objectHP > 0)
{
Destroy(other.gameObject);
GameObject effect = Instantiate(effectPrefab, transform.position, Quaternion.identity);
Destroy(effect, 2.0f);
}
else
{
Destroy(other.gameObject);
GameObject effect2 = Instantiate(effectPrefab2, transform.position, Quaternion.identity);
Destroy(effect2, 2.0f);
Destroy(this.gameObject);
// ★改良
// ランダム機能を使う
// 「Length」というプロパティを使うと、配列の要素数(「幾つの」データが入っているか)を測ることができます。
GameObject dropItem = itemPrefabs[Random.Range(0, itemPrefabs.Length)];
Vector3 pos = transform.position;
// ★改良
// itemPrefabをdropItemに変更する。
Instantiate(dropItem, new Vector3(pos.x, pos.y + 0.5f, pos.z), Quaternion.identity);
}
}
}
}
![04061b40 e5a5 4c05 b7ed cb2378184801](https://codegenius.org/uploads/slide/image/2163/04061b40-e5a5-4c05-b7ed-cb2378184801.jpeg)
![D509218b 587a 4e03 872a 9188c70b21ec](https://codegenius.org/uploads/slide/image/2164/d509218b-587a-4e03-872a-9188c70b21ec.jpeg)
![00dc328a 8548 4bae 8452 09fa4e8f3bbe](https://codegenius.org/uploads/slide/image/2165/00dc328a-8548-4bae-8452-09fa4e8f3bbe.jpeg)
アイテムをランダムに出現させる