I’m referencing a scriptable object in one of my scripts for some stuff my gameplay will constantly reuse it (stuff that will be spawned when destroying some props) and everything works well on the editor but in the build the reference to the scriptable object becomes null so I can’t access, I get a nullref error in the exe’s console.
I suspect you’re accidentally orphaning the object and it’s getting garbage collected.
This has happened to me in cases like this: I initialise a list of items with one item and create a reference to the desired object. Later, I add more items, which get their own reference from the previous object. At some point, I empty the list, which means the object isn’t being referenced… it gets garbage collected, which I don’t notice because nothing is using it… and then I add a new object to the list, which has a null reference to that object.
Since I didn’t notice the list was empty, I don’t do the magic dance of making the reference by hand, and I may even populate the list with more items that copy the null reference before it actually gets used and everything falls apart.
When you’re running in the editor, however, the editor is probably holding a reference to the script in the inspector or something and this doesn’t happen.
Does that help? I’m shooting in the dark here, but I’m occasionally good at psychic debugging.
@cdarklock It does help, the more info the best, I think I also did that a couple of times too, you select the object, see on the inspector and everything in there, referenced, but in the build things are different!
But I forgot to reply here telling that the problem was solved, I don’t know what happened exactly, I was getting a nullref to my scriptable object (which is being referenced through the inspector by dragging and dropping in a scriptableobject field, no secrets there) and in the editor I was being able to access that scriptable object, but in the build I was getting nullref! I don’t know exactly what fixed this because I did many things, but I suspect the nullref stopped when I recreated the scriptableobject asset, I deleted the old one and created a new one, what is weid though is that I don’t recall changing anything in the scriptable object class.
It’s really hard to tell things when this kind of bug only happens in the build, I had this same kind of problem when I had a character and I had no idea why he wasn’t animating when he was off the screen in build, but in the editor he was being fine, it turns out that in the editor he was not visible in the game view… but he was in the scene view, that’s why everything was fine in the editor but not in the build, so I set the animator to “always animate” and that did it. Lessons learned.
Thanks for replying guys. If anyone is having this, works in the editor but not in build, try to delete and create a new scriptableobject asset.
I had null references in build as well, the problem was different name of SciptableObject class and *.cs file, all worked in editor though. After renaming and recreating asset all works fine
I was having the same issue. I was using a OnValidate() To populate an array(because array is much faster to loop through) from a list. While I can see in the editor, that the array has been correctly populated, I was getting a null exception when the project was built.
Ah, just a reminder OnValidate doesn’t run on builds (at least, Unity doesn’t send this message automatically on builds), only on editor, if you were counting on OnValidate to do something on the build, then it will not work, if you run OnValidate in the editor and leave your array/list fulfilled ok, but if you leave it empty (like, for example, not serializing it), it will not work at all…
no… I am not counting on it to do anything on build -I am aware that OnValidate() only gets called in editor. I use it to populates things in editor. This worked fine for me all the other data, like when populated an ID it stayed after the build. And I can confirm that it is NOT empty before the build.
I haven’t done much in depth testing, but arrays seem to be the only thing that seem to lose data. ints, floats, strings, and data on classes hold just fine. Maybe something has to do with the way arrays get serialized.
I ran into another similar issue with not holding data. Data(ScriptableObject asset) would disappear as soon as I restarted the editor or did a build. Guess I didn’t understand how unity does it’s serializing and de-serializing. This article helped greatly to see what was happening (https://blogs.unity3d.com/2012/10/25/unity-serialization/). Fix was simple. I had to make sure the private fields gets serialized simply by adding [SerializeField] and it held the data, during builds.
I had thought this was not necessary since it was holding in the editor with ScriptableObjects. But per that article, everything get serialized and then de-serialized, hence anything that’s not nailed down (e.g. public or doesn’t have [SerializeField]) gets reset to default when you reopen Unity or build -read the article, it explains this better than I ever can
Ran into the original issue in this post, scriptable objects working in editor but throwing nulls in a build due to losing a reference to the original .cs file (possibly caused by duplicating the .asset instead of creating a new one). I fixed the issue by setting the inspector to debug mode and reassigning the .cs to the scriptable objects, didn’t even have to recreate them!
In scriptableobject , Script option(click on object ) is null. we are remove the script of scriptableobject and create new script again. After that we found script option have same script name which one you created
Watch your scriptableObject asset. In script section, you may see none. Your classes names and files names must be the same. Change the name and again create the SO.
In editor it is OK but in the build, you get the null reference error.
In my case I was trying to work on a unity project stored in a Dropbox folder…DON’T DO THIS. Dropbox seems to corrupt asset files being worked on. recreated the scriptable objects not in a dropbox folder fixed it for me.
i too had the same problem,i got my solution from below steps.
first check your scrip-table object is referring to the script or not.
if it is not referring check the .cs file name and your class name(they should be same)
if the object is not referring to it’s parent script- in editor it will works fine but in build you face the problem.