Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Issues with ScriptableObject

Discussion in 'Scripting' started by thephox1982, Sep 7, 2023.

  1. thephox1982

    thephox1982

    Joined:
    Jan 21, 2022
    Posts:
    20
    Hello,

    This may or may not be a bug but sure seems like one..

    Since the creator of the asset hasn't gotten back to me in over a week and I seem to have isolated the issue to Unity 2021.3 ScriptableObject, I'll ask here my question.

    Code (CSharp):
    1.  
    2.          Debug.Log("MARK 1");
    3.             var assetLoaderOptions = ScriptableObject.CreateInstance<AssetLoaderOptions>();
    4.          Debug.Log("MARK 2");
    5.  
    I simply added Debug.Log lines before and after where the issue seems to be located, MARK 1 shows up in console, MARK 2 does not, thus ScriptableObject.CreateInstance is having an error of some kind.

    I have tried reimporting entire project by deleting Library folder, I have also tried a couple different versions of 2021.3. Also, I have a project for the server side of things and it uses same Unity version and uses the same asset and that works 100% flawlessly!

    This seems to suggest something is missing from Unity in my main project, but from everything I can tell, ScriptableObject is built-in to Unity and should just work without adding any packages.

    Anyone have any clue to what could be happening here? My entire project is dead in the water due to this single line of code which works in one project but not another setup the entirely same way, the server code is actually a direct copy of the same CS file with same using statements and everything yet it works and the main game project code doesn't.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Perhaps wrap it in a Pokemon "gotta catch 'em all" try/catch and see if it's barfing?

    Someone calling this might already be Pokemon-ing it, which is always irritating when people try to be this clever.

    ALSO: try changing "MARK 1" to "MARK 1 FOOBARDOODLES" and make sure that's the MARK 1 you think it is.
     
  3. thephox1982

    thephox1982

    Joined:
    Jan 21, 2022
    Posts:
    20

    I believe I have tried the try/catch and got nothing, I will however try this again and post back results once I have some time free, cheers!
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Try some other versions of Unity and if a stripped-down version of this works in one version and not the other, file a real bug with Help -> Report A Bug

    Also, make another SO class, like PhoxSO, and make one of those. Does it work?

    Just the usual bisect, bisect, bisect until you are holding the guilty party by the left earlobe.
     
  5. thephox1982

    thephox1982

    Joined:
    Jan 21, 2022
    Posts:
    20
    I did actually try in other classes with same result, it will not run in any cs file even in the most basic application either within the project, this is why I am so confused on what is going on, it works perfectly in the server project using same Unity version literally the same file line for line but not in this project, I am completely baffled, this is why I suspected something some where is missing with Unity that ScriptableObject requires yet come find out that is built right into UnityEngine.Core so that shouldn't be the case at all.

    I just ran it in a try and I apparently didn't do so before and am getting this error:

    UnityEngine.UnityException: CreateScriptableObjectInstanceFromType can only be called from the main thread.
    Constructors and field initializers will be executed from the loading thread when loading a scene.​

    That is odd, nowhere in my code leading up to the calling method is using threading nor do I have

    using System.Threading;​

    What is even happening here!?
    Is there anything else that could cause that to happen?
     
  6. thephox1982

    thephox1982

    Joined:
    Jan 21, 2022
    Posts:
    20
    cache.ReadyEvent += (object o, EventArgs a) => { };

    I missed that.. it's being called inside of an event which uses threading!

    The server code has this same bit of code but is not yet using it and thus why that project has it working and my main game project doesn't. Oops.

    Now the fun part, since I need to call the method from the ready event.. how to do so from the main thread..
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    Hm, good question. This is an editor script I take it?? If so perhaps a combination of the Editor Coroutines package and something like Thread Ninja??
     
  8. thephox1982

    thephox1982

    Joined:
    Jan 21, 2022
    Posts:
    20
    This is a runtime script.

    From what I can tell searching on how to call methods on main thread from tasks and EventHandlers it is just not doable, so now I have to throw out of months worth of work on a caching system or find another way to go about it. I guess this particular issue is resolved though now that I found the cause.

    Thanks for the assistance!
     
  9. thephox1982

    thephox1982

    Joined:
    Jan 21, 2022
    Posts:
    20
    What I ended up doing in case anyone wonders how to call method on main thread from an event or thread, just set a flag and then in Update() check the flag, if it is true set it to false and then call the method right after doing so!
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563