ObjectPoolを汎用的に扱えるようにGenericで記述しました。それを用いて、弾の発射、管理をするクラスを作成しました。現状ですと、start関数でシーン再読み込み時にまた別のインスタンスを生成してしまいますがここで生成したインスタンスを別のシーンに行ったとき(再読み込みも)にも使用したいと考えております。
staticを使えばシーンをまたいでも保持できると知り、色々試して見たもののうまくいきません。
下記のスクリプトですとエラーが出てしまいます。どうすれば、作成したインスタンスをシーンをまたいでも保持し、オブジェクトを再利用していけるようになるでしょうか?staticの使い方の理解がいまいちなのでご教授いただけると助かります。
error
1MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it. 2Your script should either check if it is null or you should not destroy the object. 3ObjectPool`1[Enemy].GetObject () (at Assets/Scripts/ObjectPool.cs:36) 4PoolEnemy.Update () (at Assets/Scripts/PoolEnemy.cs:25) 5
if (!_objectList[i].activeInHierarchy)ここのことを指しております
objectpoolclass
1using System; 2using System.Collections; 3using System.Collections.Generic; 4using System.Linq; 5using Boo.Lang.Environments; 6using UnityEngine; 7 8public class ObjectPool<T> where T : MonoBehaviour 9{ 10 11 private List<GameObject> _objectList; 12 private GameObject _objectInstance; 13 private PrimitiveType _type; 14 public int _objectLimit; 15 public static ObjectPool<T> _instance; 16 17 //弾を非アクティブに生成する 18 public ObjectPool(int objectAmount,PrimitiveType type) 19 { 20 _type = type; 21 if(_objectList==null) _objectList = new List<GameObject>(); 22 for (int i = 0; i < objectAmount; i++) 23 { 24 _objectInstance = GameObject.CreatePrimitive(_type); 25 _objectInstance.SetActive(false); 26 _objectInstance.name = typeof(T).Name; 27 _objectInstance.tag = typeof(T).Name; 28 _objectInstance.AddComponent<T>(); 29 _objectList.Add(_objectInstance); 30 } 31 } 32 //弾をアクティブにして、足りなくなったら生成する 33 public GameObject GetObject() 34 { 35 for (int i = 0; i < _objectList.Count; i++) 36 { 37 if (!_objectList[i].activeInHierarchy) 38 { 39 _objectList[i].SetActive(true); 40 Debug.Log(_objectList[i].GetInstanceID()); 41 return _objectList[i]; 42 } 43 44 if (_objectList.Count < _objectLimit) 45 { 46 GameObject _newObjectInstance = GameObject.CreatePrimitive(_type); 47 _newObjectInstance.name = typeof(T).Name; 48 _newObjectInstance.tag = typeof(T).Name; 49 _newObjectInstance.AddComponent<T>(); 50 _objectList.Add(_newObjectInstance); 51 return _newObjectInstance; 52 } 53 } 54 55 return null; 56 } 57 58 59} 60 61
ShootBulletclass
1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5public class ShootBullet : MonoBehaviour 6{ 7 8 private ObjectPool<Bullet> ObjectPool; 9 10 // ObjectPoolのBullet型インスタンスの生成 11 void Start () 12 { 13 if (ObjectPool<Bullet>._instance == null) 14 { 15 ObjectPool = new ObjectPool<Bullet>(10, PrimitiveType.Sphere); 16 ObjectPool<Bullet>._instance = this.ObjectPool; 17 ObjectPool<Bullet>._instance._objectLimit = 20; 18 } 19 20 } 21 22 // 弾を発射する 23 void Update () { 24 25 if (Input.GetMouseButtonDown(0)) 26 { 27 Ray rayOrigin = Camera.main.ScreenPointToRay(Input.mousePosition); 28 Vector3 direction = rayOrigin.direction; 29 GameObject bullet =ObjectPool<Bullet>._instance.GetObject(); 30 bullet.GetComponent<Rigidbody>().velocity = direction * 20; 31 } 32 } 33 34 35} 36
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/10/23 05:42