Search Unity

Reference Scriptable Object Outside of MonoBehaviour at runtime

Discussion in 'Scripting' started by acmshar, Dec 28, 2018.

  1. acmshar

    acmshar

    Joined:
    May 4, 2010
    Posts:
    29
    The problem I am trying to solve: I have a non-MonoBehaviour class of static methods that send data from my game to a database. I recently introduced a build script that sets values on a Scriptable Object for things like the API URL for my database depending on my build target (local development vs production). I would like the database class to read those values in a build of the game.

    I would prefer not to instantiate the database class, load the asset from Resources (I don't want to include that data so easily visible in the build), pass a reference to the SO from other MonoBehaviour scripts because it would require doing so in ~20 other scripts (though this is probably what I'll do if I can't figure out a more elegant solution).

    Is there another way for me to access that scriptable object in the non-MonoBehaviour class at runtime (in a build, not editor)? I'm also open to solutions to the original problem (swapping out values depending on build target) that don't use Scriptable Objects if that simplifies matters.