How to reference a scene object with a ScriptableObject? (ExposedReference)

I have these gameObjects pools 9403010--1316294--upload_2023-10-11_14-53-37.png
I want these pools to be accessible anywhere via code and inspector.

Now of course you can’t drag and drop scene objects when using a ScriptableObject BUT I ended up finding the existance of “ExposedReference” which seem to be what I’m looking for.

The problem is that litterally nobody is talking about it. No videos or guides anywhere, and the unity doc use it with the timeline feature which i don’t want.

If somebody show a simple example on how to use it without the timeline feature that would be cool :slight_smile:
If you don’t know about “ExposedReference”, but know how to reference scene objects anywhere via code and inspector that’ll would be cool too.

9403010--1316294--upload_2023-10-11_14-53-37.png

Are you sure you want to do this?

The costs and issues associated with object pooling / pools:

https://discussions.unity.com/t/892797/10

https://discussions.unity.com/t/833104/2

I dunno about that newfangled thing.

Locating stuff at runtime is really well-traveled territory. Just use one of “the usual” approaches.

Remember the first rule of GameObject.Find():

Do not use GameObject.Find();

More information: https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html

More information: https://discussions.unity.com/t/899843/12

In general, DO NOT use Find-like or GetComponent/AddComponent-like methods unless there truly is no other way, eg, dynamic runtime discovery of arbitrary objects. These mechanisms are for extremely-advanced use ONLY.
If something is built into your scene or prefab, make a script and drag the reference(s) in. That will let you experience the highest rate of The Unity Way™ success of accessing things in your game.

Here’s a short tutorial on ExposedReference.

https://www.youtube.com/watch?v=KFFCGABCH3c

Sadly not as trivial as one would wish it to. In Unity there is just a very harsh border between scenes and assets. SOs are assets.
To overcome that border, one needs a class that implements IExposedReferenceTable. You use that one then to create a “table of references” from your scene, which is needed by the .Resolve call of your ExposedProperty.

This is actually not a newfangled thing but as old as Asset bundles since those are their main/initial usecase it seems (gosh, I barely recognize Unity in that video :smile: )

For “normal” situations I would rather avoid usecases where I need to couple an SO to the scene this tightly.

A classic approach is to use a reference provider singleton object in the scene. You cannot drag that one to an SO either but in code you can access the references. So use the SOs the way they are intended to: As configuration assets where you write standalone data (numbers, strings, other SOs, prefabs, but not references in scenes).
Scenes and assets just are not supposed to be firmly coupled and therefore it’s impossible to have something from a scene, accessible truly anywhere with the inspector.

I think I’ll end up just not using pools, that way I can make a simple prefab reference which is referenceable both in code and in inspector. Thanks for your replies :slight_smile:

Make sure to use the profiler in that case and check the performance, especially on your target hardware (not just your dev machine)!
Projectiles and explosions pretty much are two really common usecases of object pools for a reason after all.