I have a pooler component that takes in a generic type. So, if I want a pooler for SpriteRenderers, I can inherit the generic class with ObjectPooler and it will make a pooler of SpriteRenderers.
Problem is, when I build it to Windows Standalone, the public field “Prefab” doesn’t seem to be filled. So, it throws an error. It works completely fine in the Unity Editor, but when I build it, it seems to lose the serializing of the field and sets it to null.
Any idea what might be causing this behavior?
using System.Collections.Generic;
using UnityEngine;
public class ObjectPooler<T> : MonoBehaviour where T : Component {
#if UNITY_EDITOR
[Header("Debug")]
public bool Request = false;
public Vector2 Position;
private void Update() {
if (!Request) return;
Request = false;
T prefab = Get();
DebugSpawned(prefab);
prefab.gameObject.SetActive(true);
}
protected virtual void DebugSpawned(T prefab) {
prefab.transform.position = Position;
}
[Space]
#endif
public T Prefab;
public readonly HashSet<T> CreatedPrefabs = new HashSet<T>();
public T Get() {
foreach (T item in CreatedPrefabs) {
if (!item.gameObject.activeSelf)
return item;
}
T newPrefab = InstantiatePrefab();
CreatedPrefabs.Add(newPrefab);
return newPrefab;
}
protected virtual T InstantiatePrefab() {
return Instantiate(Prefab);
}
}
using UnityEngine;
public abstract class TileSpritePooler<T> : ObjectPooler<T> where T : TileSprite {
public GameManager GameManager;
public RegenerateCompositeCollider RegenerateCompositeCollider;
public Transform Parent;
protected override T InstantiatePrefab() {
T tileSprite = base.InstantiatePrefab();
tileSprite.transform.SetParent(Parent);
tileSprite.Initialize(GameManager, RegenerateCompositeCollider);
return tileSprite;
}
}
public class GroundTileSpritePooler : TileSpritePooler<GroundTileSprite> {
}