Why "the associated script can not be loaded" for some Scriptable Objects?
I created several scriptable objects that I use in a finite-state-machine. They all work fine except about every 4th or 5th "play" (or whenever I close and reopen my project) the same 3 scriptable objects are no longer valid. Their "script" slot is populated with "None (Mono Script)", and the warning underneath reads, "The associated script can not be loaded. Please fix any compile errors and assign a valid script." As a result, I have to recreate these 3 objects periodically. They compile fine, and--aside from their names--are identical to a few other scriptable objects in my solution that work fine and never disappear. So, I'm confused. Anybody know what would cause this, so I can stop recreating these 3 objects every 4-5 test runs of my game? Thanks!
Here's a screenshot of the error:
Here's the code of one of the 3 scriptable objects:
I should add that I'm using these scriptable objects as Pluggable AI objects. I'm noticing the 3 troublesome objects almost always disappear on reopening Unity, and they also disappear whenever a scene changes in unity, and they even sometimes disappear every 4-5 runs of my game in unity. The files/scripts themselves still exist (so I don't have to rewrite the scripts), but they are no longer associated with the Pluggable AI object as shown in the screenshot above (where it says "None ($$anonymous$$ono Script)" where it should say the name of my object, such as "DistributeCardsToOpponent3CompleteDecision".
Are you using the ScriptableObjects as runtime-modifiable assets or creating instances of them at runtime?
I'm not creating instances at runtime. They are Pluggable AI scriptable assets that exist prior to runtime, and then I just select which ones I want to execute in a given state via the Inspector. It's the exact same process that $$anonymous$$att Shell used in his Unity Live Training demo called "Pluggable AI With Scriptable Objects", which you can find here: https://www.bing.com/videos/search?q=unity+plugable+ai+with+scriptable+objects&&view=detail∣=2F1465C1BD0149A3B3962F1465C1BD0149A3B396&rvsmid=8D607F5442D905DF8B8D8D607F5442D905DF8B8D&fsscr=0&FOR$$anonymous$$=VDRVRV.
The error is "NullReferenceException: Object reference not set to an instance of an object State.DoEnterActions (.StateController controller) (at Assets/PluggableAI/Scripts/State.cs:20)". It's because the reference to the script behind the scriptable object has been broken, which means the "Decision" element in my State object is "None (Decision)" ins$$anonymous$$d of "Distribute Cards to Opponent 3" as it should be (see screenshot). So, Unity doesn't think there's an object. I have to recreate the object, then drag it back into the "Decision" element box in my state in the Inspector. I have to repeatedly do this every time the scriptable object gets removed (as shown in my original screenshot above). Hopefully that makes sense.
I've also included a screenshot of the line of code that throws the error, because it's loaded a null value into my "Actions" array, so it can't read it at line 20. But that's not the root-problem. That line of code works once I've recreated my scriptable object. $$anonymous$$y question is why does my scriptable object keep resetting to "None ($$anonymous$$ono Script)" every few runs of my program, causing all these problems until I recreate it?!
If you want to update / improve your question, you should edit your question. You can edit your question from the cog-wheel menu at the top right of your question.
Answer by Bunny83 · Jul 30, 2017 at 02:47 AM
It's hard to tell what could be the reason, however common problems are:
your scriptable object is not located in it's own script file but placed in a file with multiple other classes
the name of the script file does not match the actual class name.
Your project looks quite organised so i would doubt the first point. So most likely the file name doesn't match the class name. Note by "file name" i mean the script file (*.cs) not your scriptable object asset file.
ps: If you have many classes you may want to start using namespaces. Unity does support namespaces for quite some time now.
Also next time it would be better to include code samples as text and mark it as code instead of posting images. It just makes it more difficult for us to read and refer to your code.
Yes, that's it! THAN$$anonymous$$ YOU! The problem was a mismatch between the names of the classes and the file names. I inadvertently had an extra "e" in the those 3 titles that escaped my eye (e.g., RepeateDistributionOfCardsCompleteDecision).
It works for me too! Thank you! $$anonymous$$uch appreciated!