- Home /
Generic Pooler not saving serialized fields on build?
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> {
}
Answer by Bunny83 · Dec 31, 2020 at 03:32 PM
That's very unlikely. You are probably missing something in your code which btw we have never seen. So we can't tell you what may be wrong with it. If it serializes in the editor it would also work at runtime. Unity always was a bit picky with generic types. Do you create a concrete derived version or do you somehow directly use the generic type?
Without seeing any code this question is a dead end. Feel free to edit your question and add more details.
edit
You should avoid having serialized fields with editor only conditional tags. This will mess up the serialized data that is serialized during edit mode and the class structure does not match the class at runtime. While it may work in some cases it's generally not recommended. It may even depend on the order of the fields.
Thank you! I commented out the code in the conditional tags, and it worked! So, no more conditional serializing data. Understood!
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Subsequent builds getting smaller.,Subsequent builds taking less space? 1 Answer
Will my paid assets appear in the final game build? 1 Answer
Is it possible to prevent AssertionComponent from being stripped out of non-dev builds? 1 Answer
some builds crashes on level change 1 Answer