Search Unity

My ScriptableObject references are becoming null in the build

Discussion in 'Scripting' started by Rodolfo-Rubens, Apr 3, 2017.

  1. Rodolfo-Rubens

    Rodolfo-Rubens

    Joined:
    Nov 17, 2012
    Posts:
    1,197
    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.

    Any ideas?

    Thanks in advance.
     
  2. Chrysto

    Chrysto

    Joined:
    Jun 28, 2016
    Posts:
    16
    same happened to me after moving to 5.6... on 5.5.0f3 everything was working fine...
     
    DezBoyle and Rodolfo-Rubens like this.
  3. cdarklock

    cdarklock

    Joined:
    Jan 3, 2016
    Posts:
    455
    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.
     
  4. Rodolfo-Rubens

    Rodolfo-Rubens

    Joined:
    Nov 17, 2012
    Posts:
    1,197
    Hey guys! Thanks for replying!

    @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.
     
  5. kumaiga

    kumaiga

    Joined:
    Nov 25, 2012
    Posts:
    2
    I also solved the same problem by recreating a new scriptableobject asset.
    Thanks good information!
     
    Alaadel likes this.
  6. c0ffeeartc

    c0ffeeartc

    Joined:
    Jul 23, 2015
    Posts:
    42
    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
     
    Emolk, Alaadel, JordanLiver and 10 others like this.
  7. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    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.

    I had to add an extra condition:

    Code (CSharp):
    1. if(partsArray==null)
    2. {
    3.       partsArray = partsList.ToArray();
    4. }
    Then it worked. I am curious to know what this is happening. Some how partsArray goes back to (reset) null.
     
    Alaadel likes this.
  8. Rodolfo-Rubens

    Rodolfo-Rubens

    Joined:
    Nov 17, 2012
    Posts:
    1,197
    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...
     
    Alaadel likes this.
  9. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    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.
     
  10. FlightOfOne

    FlightOfOne

    Joined:
    Aug 1, 2014
    Posts:
    668
    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 :)
     
    Last edited: Aug 4, 2018
  11. Wazimog

    Wazimog

    Joined:
    Dec 7, 2012
    Posts:
    2
    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!
     
  12. Hyperwolfs

    Hyperwolfs

    Joined:
    Jun 14, 2017
    Posts:
    1
    I found the solution !!!!!!!!!!!

    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

    it's work for me !!
     

    Attached Files:

    ubo5 likes this.
  13. shawnblais

    shawnblais

    Joined:
    Oct 11, 2012
    Posts:
    324
    Ya I had this same issue when I had my SO class embedded in another class file that didn't match the name.

    Moved it to it's own class, with a proper file name, and it seems to be all better.
     
    ArachnidAnimal likes this.
  14. mahdiii

    mahdiii

    Joined:
    Oct 30, 2014
    Posts:
    856
    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.
     
    Screenhog and ArachnidAnimal like this.
  15. dan2600

    dan2600

    Joined:
    Apr 18, 2019
    Posts:
    1
    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.
     
  16. parkmist

    parkmist

    Joined:
    Jul 23, 2014
    Posts:
    5
    I had this issue, what fixed it for me was an un-initialized List.
    So, instead of this:
    List<GameObject> m_Objs;

    You do this:
    List<GameObject> m_Objs = new List<GameObject>();
     
    Azim04 likes this.
  17. goutham12

    goutham12

    Joined:
    Jul 14, 2016
    Posts:
    53
    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.
     
  18. Tornar

    Tornar

    Joined:
    Dec 28, 2020
    Posts:
    87
    Add [SerializeField] to my scriptObjects list did it started to work successfully in builds.

    I know it is an old thread but it's one of the first google results when searching info about this problem.

    Thanks!