Hi

I have a CustomEditor script which calls a method on a MonoBehaviour script. This method gets called while in edit mode only so the game is not running yet. Inside this method I'm trying to get access to a combatant settings (its default prefab). I thought I could use the value of ORK.Combatants.Get(orkID).prefab.prefab but it's an AssetSource. However I need the GameObject to run GetComponent<>() on it.

I'm pretty sure there is a way to access the Combatant's prefab GameObject outside of the Play mode but can't figure how :-(
  • Use this:
    GameObject prefab = ORK.Combatants.Get(index).prefab.prefab.StoredAsset;

    AssetSource is a wrapper for the asset selection system (reference, asset bundle, etc.), accessing the StoredAsset will get you the asset itself. Naturally, this'll return null if no asset was selected yet.
    Please consider rating/reviewing my products on the Asset Store (hopefully positively), as that helps tremendously with getting found.
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
  • edited August 2023
    Maybe I missed something ... it does not contain a definition for StoredAsset

    AssetSource' does not contain a definition for 'StoredAsset' and no accessible extension method ....

    Should I add a using directive ? Just to make sure, we are on ORK2
    Post edited by frednorm2000 on
  • edited August 2023
    Ah, ORK 2 can do it like this (ORK 3 can also do it that way):
    GameObject prefab = ORK.Combatants.Get(index).prefab.prefab;
    The code has automatic casting capabilities to handle that for you :)
    ORK 3's StoredAsset is just a bit better for performance as it doesn't need to do some checks and loads each time.
    Post edited by gamingislove on
    Please consider rating/reviewing my products on the Asset Store (hopefully positively), as that helps tremendously with getting found.
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
  • This works fine during Play mode. I'm assuming that hitting "Play" load ORK's database in memory.
    It also works event after I hit "Stop". I'm assuming that the database remains loaded untill the gc do it's job ?! Which does not occur event if I hit "Stop".

    However, if the game is stopped and then I recompile, the instruction is not working anymore. ORK.Combatants is null. I am assuming that recompiling the project triggers the gc and ORK's database get unloaded from memory. At this point I'm not sure what is going on under the hood.

    But one thing I'm sure is that during Play mode or right after I hit "Stop", the instruction ORK.Combatants.Get(index).prefab.prefab works totaly fine. But as soon as it recompile, it failed because ORK.Combatants throw error "System.NullReferenceException:Object reference not set to an instance of an object".
  • Yeah, that's true - either open the ORK editor once before using it, or initialize ORK via code.
    This only works for editor code:
    if(!ORK.Instantiated)
    {
    ORK.Initialize(ORKAssetHelper.LoadORKProject());
    }

    Otherwise, you can use the same with a provided ORK project asset outside of editor code (e.g. have the asset as a regular asset field in your code):
    if(!ORK.Instantiated)
    {
    ORK.Initialize(projectAsset);
    }
    Please consider rating/reviewing my products on the Asset Store (hopefully positively), as that helps tremendously with getting found.
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
  • Alright sounds like a promising solution. Just need to figure out why we don't have ORKFrameworks.Editor in our project because ORKAssetHelper does not exist in our current context. Namespace Editor does not exist in the namespace ORKFramework
  • Just like other Unity editor stuff, that's only available in scripts that are under an Editor folder.
    Please consider rating/reviewing my products on the Asset Store (hopefully positively), as that helps tremendously with getting found.
    If you're enjoying my products, updates and support, please consider supporting me on patreon.com!
  • Awesome thank you !!
Sign In or Register to comment.