GameObject.setActive not triggering within scriptable Objects

Hey,

So I have a scriptable object that references a prefab within my resource folder. Within the scriptable object, I set the UI components at runtime.



As shown in the first image, the transforms are being populated accordingly, but whenever I try to do a transform.gameObject.setActive(true/false), the game object within the prefab doesn’t respond.

I tried referencing the scriptable object from a separate script, but the results were the same. I was wondering if this was the intended behavior. Let me now if more information is needed

Scriptable Objects can only permanently refer to Prefabs on disk.

Prefabs are Instantiated into a scene, making a fresh “live” instance.

Instantiate returns a reference to the instance that it just created.

You must use that Instance reference to do anything, not the reference to the prefab.

Generally you do NOT store instance references in a ScriptableObject because the moment the scene goes away, those references become invalid (null).

For future reference, photographs / screencaps of code are not a thing.

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

1 Like

Thanks @Kurt-Dekker , that makes sense; I thought I was being clever using scriptable objects to avoid reptitive code :stuck_out_tongue:

Also thanks for the heads up for future post, i’ll be sure to use code tags next time.

Cheers.

That’s actually legit… I’ve seen systems that have a ScriptableObject which is used as a master reference, such as to a prefab, and contains code to make an instance of it.

You technically CAN even use the ScriptableObject to store this reference (obviously in a different variable), but it will never be persisted to disk. In that case, for clarity, you might want to either make it private or otherwise decorate it with [System.Nonserialized] for instance.

More likely is you pass this Instance reference onto something else that manages runtime stuff and keep the SO as pure “master copy of everything” purposes.

1 Like