今回は僕がゲームを作成していく上であまり情報がなかったゲームオブジェクトを配列に入れる方法に関して解説していきます。
皆さんの参考になればうれしいです。
Advertisement
方法1
方法1は、配列に当てはめたいゲームオブジェクトをスクリプトによって探し出し、配列に当てはめる方法です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Array1 : MonoBehaviour { void Start() { // 型名 配列の名前 型名[配列の数] GameObject[] MonsterBox = new GameObject[3]; // 型名[番号] 代入するゲームオブジェクト MonsterBox[0] = GameObject.Find("monster1"); MonsterBox[1] = GameObject.Find("monster2"); MonsterBox[2] = GameObject.Find("monster3"); } } |
方法2
方法2は、Inspectorウィンドウに割り当てる場所を指定数作成し、配列に割り当てる方法です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Array2 : MonoBehaviour { public GameObject monster1; public GameObject monster2; public GameObject monster3; // 型名 配列の名前 GameObject[] MonsterBox; void Start() { // 配列の名前 型名 代入するゲームオブジェクト MonsterBox = new GameObject[] { monster1, monster2, monster3 }; // MonsetrBox[0]=monsetr1 } } |
方法3
方法3は、Inspectorウィンドウで配列の数をしてでき、その後配列に割り当てる方法です。
1 2 3 4 5 6 7 8 9 10 |
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Array3 : MonoBehaviour { public GameObject[] MonsterBox; } |