Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by galactichyperstar · Dec 31, 2020 at 03:26 PM · buildpoolingserializegenericsbuilds

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> {
 
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image galactichyperstar · Dec 31, 2020 at 03:47 PM 0
Share

Updated with code!

avatar image galactichyperstar · Dec 31, 2020 at 05:20 PM 0
Share

Thank you! I commented out the code in the conditional tags, and it worked! So, no more conditional serializing data. Understood!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

124 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges