Using Scriptable Objects to Load Scenes

Hey all! So I was experimenting to see if I could use a Scriptable Object (SO) to use as the button’s object, and it worked! My question is this: Would there be a benefit or disadvantage to using SOs? Or is it just another option that could be used if desired?

Thanks in advance!

Hi,

Could you share a screenshot, please? Actually, Scriptable Objects do not have a transform and no “body” on which you could click. That’s why I’m slightly confused at the moment.

Screenshot 1) I create a ScriptableObject with the LoadNextScene() method and create a SceneLoader from it in the assets folder.

Screenshot 2) From there, I can drag that SceneLoader directly into the button’s OnClick function.

By doing it this way, there is no need to create a SceneLoader GameObject.

But there must be a Success Button in the Hierarchy/scene which is linked to the prefab. Otherwise, the changes you make to the prefab will not get applied to the Success Button in your Hierarchy/scene.

I’m not sure I understand… The button in the hierarchy is attached to the prefab. The screenshot below shows the button’s prefab in the inspector. It’s OnClick() calls the LoadNextScene() method from the scriptable object (SceneLoader).

By doing it this way I can instantiate the button from the prefab with the SceneLoader functionality already attached to it… no need to create a SceneLoader GameObject. I can upload to GitHub or upload a zip folder of the project if you would like.

There is a game object named “Sucess Button” in your Hierarchy. It is not your prefab. It is just linked to it.

Right, I understand that it is linked to it. My question is not about the prefab… I understand the relationship between the prefabs and the GameObjects linked to them.

My question is about using the Scriptable Object instead of a GameObject with a script attached. The only difference I have is that the Scriptable Object can be applied to the button directly, whereas the code in the lectures must be attached to a game object before the button can call it. Due to this, it seems that the Scriptable Object allows me to not need to remember to add a scene loader game object to every scene since the functionality can be applied directly to the button.

I am just asking is there an obvious downside to doing it this way? I’ve nearly completed the section at this point and everything seems to work fine, but I don’t want to learn things incorrectly by doing them differently.

I hope this has made sense, and I apologize for any misunderstandings.

If it works, it works. :slight_smile:

If your Success Button in your scene references something in your Assets folder and it works, keep your solution. As far as I’m aware, the script itself does not work in the OnClick() field of the button but maybe that was changed in newer versions of Unity.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms