SerializeField on Scriptable Object

Why cant I drag my Pool Object onto my SerializeField Pool filed on a scriptableObject. Seems to work with a MonoBehaviour. Please help.

public class Test :  ScriptableObject {

    [SerializeField]
    public Pool pool;  // 

}

You can’t serialize references to stuff in scenes on prefabs or ScriptableObjects because that thing might not exist when the prefab/SO is created.

Mm what’s my best alternative here, a name to the object?

No - that’s probably the worst thing you could do :slight_smile:

Why does the SO need a reference to a scene object in the first place? What is it doing?

Basically here are some snippet of what i was trying to achieve

[SerializeField]
public Pool pool;  // Reference in what to pool


//
// SO biodomePackages is where I go for all data to generate my scene
this.pool = biodomePackages [0].Tiles [0].ObjPool; // ObjPool is a GO with pool script & the prefab of what to pool
GameObject clone = pool.nextPoolObj;
clone.transform.position = transform.position; // Cant parant to prefab

If you’re trying to do object pooling then you’d certainly be better off implementing your pool as a singleton and letting your SO instances grab a reference to it through the static Instance property.

There’s lots of info on the net about singletons and lots of info on this forum for implementing MonoBehaviour singletons if you need it.

1 Like

Unfortunately I need an instance per pool object. The pool script handle activating and creating GO. Each instance need a unique list to keep track of pooled and inactive object.

Im out of ideas here

I’m not understanding your scenario. Why does each object need a unique pool?

Your code snippet also looks like it’s not very applicable to a ScriptableObject. You’re trying to parent to transform but ScriptableObjects don’t exist in the world.

So the parate empty GO with the Pool script contains the object that I want to pool and a list of the object that are deactivated and ready be used… This way I can duplicate the empty GO and set an object that I want to pool in this manner. If I create a single instance all the different object will be in the same pool making it very difficult to pull out the correct object

This is a modification on BoredMormon pooling tutorial. I really like how he did it. I did create a quick version where I set this up as a static Instance property but it became very difficult to manage. Im also new to SO so i hope in not misusing them

Ok - but what is the ScriptableObject doing with the pool? How are you calling whatever method is doing that thing with the pool?

You need to add the [Serializable] tag above the class definition for Pool

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BioTile{

    public string Name { get; private set; }
    public GameObject Prefab { get; private set; }
    public Pool ObjPool { get; private set; }
    public BioTile ( string name, GameObject prefab, Pool objPool ) {
        Name = name;
        Prefab = prefab;
        ObjPool =objPool;
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;



[CreateAssetMenu(menuName="ScriptableObject Assets/Packages/Biodome Package")]
public class BiodomePackage : ScriptableObject {


    [System.Serializable]public class BioTile
    {
        [SerializeField]private string _name;  
        [SerializeField]private GameObject _prefab;
        [SerializeField]private Pool _objPool;
        public string Name{get {return _name;}}
        public GameObject Prefab{get {return _prefab;}}
        public Pool ObjPool{get {return _objPool;}}
    }
    [SerializeField]private BioTile[] Ttiles;
    public BioTile[] Tiles{get {return Ttiles;}}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TileManager : MonoBehaviour {

    [SerializeField]
    public Pool pool;                                            // Reference in what to pool
    public BiodomePackage[] biodomePackages;
    public static TileManager instance = null;                    //Static instance of GameManager which allows it to be accessed by any other script.



    //
    //Awake is always called before any Start functions
    void Awake(){
        if (instance == null)
        else if (instance != this)
            Destroy(gameObject);
        DontDestroyOnLoad(gameObject);

    }



    //
    // Only in update for test will loop over biodomePackages [0].Tiles [0] to set tiles 
    void Update () {
        this.pool = biodomePackages [0].Tiles [0].ObjPool; 
        GameObject clone = pool.nextPoolObj;
        clone.transform.position = transform.position; // Cant parant to prefab
        Debug.Log (biodomePackages[0].name);
        Debug.Log (biodomePackages [0].Tiles [0].ObjPool);


        }
    }
}

Maybe I just create and store the ObjPool GO at run time, sure would be nice to see it in scene

So yeah, I really don’t get what you’re trying to do. You’ve got two different classes named BioTile. You’ve got a MonoBehaviour that references an array of ScriptableObjects and also a Pool class but each item in the SO array also references a Pool. Then the whole initial question was about serializing a Pool field which I thought was a MonoBehaviour on some empty GameObject but apparently isn’t - and then in your TileManager code you’re setting the serialized field to another serialized field from the first item in the array of ScriptableObjects.

Woof.

Yup Ive dug a hole All I wanted was a SO that contains the prefaf and pool reference. I just need a good data container

It might help you to look at his previous thread for context

He’s trying to set up a Package that would load in a set of Tile prefabs for a terrain and each package object would have a different collection of Tile prefabs. then a blank canvas terrain in the scene would then load in the package and find the tile it wants to use at it’s location. It seems he tried using LordofDuct’s SerializableDictionary but after he couldn’t get it to work he took a step back and is trying a different implementation (hence this thread).

@francois85 , the reason SerializableDictionary didn’t work as you wanted (in the previous thread) is because that Class only works for primitive types (string, floats, etc.) for its values, not full classes. I spent a great deal many hours working on that class, learning Editor Scripting, and Designing how it should look in the inspector to get it to also Draw full classes (which was later posted in the thread that SerializableDictionary was posted)

looking at the most recent code is seems you have two BioTile classes in the project. you only need one. in the example I posted previously BioTile was define in the ScriptableObject. this wasn’t required it was mostly for brevity, and the fact that I assumed no other script needed to access that class. but its perfectly fine to be defined outside the ScriptableObject.

As for this Pool business. The Pool Class that BoredMormon wrote was designed as a Monobehaviour. The data it manages lasts only in the scene thats running the script. Its not needed in the BiomePackage. The package just holds the Tiles related to its meaning. It doesn’t know nor should it care what classes plan to do with its data.

further more Pool is best on objects that are ephemeral like bullets and particles. things that spawn and despawn fast and often. I’m not exactly aware of the system you’re developing but I don’t think you’re terrain tiles will benefit much from a pooling system unless large swaths of terrain are constantly changing. At the very least you can try getting your system to work without the Pooling. Once you got the system working then you can worry about if it needs object pooling, great thing is it should be easy to add that after the fact.

Simple words to live by: “Make it Work, then Make it Right, then Make it Fast”.

Cool thanks for the help I really appreciate it. I’m not going to stop unit the freaking thing works.

don’t use any spaces in the scriptable object name.
rename it and create your objects again

Please don’t necro posts. Look at the dates of threads before posting.

Thanks.

1 Like