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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Prefabs variables reset randomly, bug?

Discussion in 'Scripting' started by Trisibo, Jun 7, 2016.

  1. Trisibo

    Trisibo

    Joined:
    Nov 1, 2010
    Posts:
    233
    I'm having a big problem with Unity, and I'd like to know if somebody has experienced the same or knows what is happening. Basically, I have been noticing since some months ago that at seemingly random times the values of some components in some prefabs just reset to the default values (for example, to 0 for ints and floats).

    I'm 100% sure this is happening, in a project I'm working now I had some prefabs with a particular component perfectly initialized, and just a moment ago all its values went to 0. The curious thing is that it happened on all the prefabs with that component, except one which conserved the values. What I'm not sure about is whether it happens only on prefabs, may also happen with other objects in the scene.

    This has happened me in two different projects, and I have no idea what triggers it. Obviously, I don't change the values anywhere in code, the only thing I've done this time is modifying the component script, which didn't have any errors, test in the editor, and then make a build for Android to test. I have no idea in which step it happened. At this point I just feel that I cannot trust Unity and that I have to be checking everything every time I make some change, and it's driving me completely crazy.
     
  2. gorbit99

    gorbit99

    Joined:
    Jul 14, 2015
    Posts:
    1,350
    It sounds like you deleted and readded a variable, or renamed it, because thats when thus should happen, but not with every variable. Since unity is quite unstable since 5.0, this can really easily be a bug
     
  3. Trisibo

    Trisibo

    Joined:
    Nov 1, 2010
    Posts:
    233
    That's what I thought at first, because actually I renamed the variables. However, I used the "FormerlySerializedAs" attribute so that the values weren't lost, and it worked fine. Also, it wouldn't leave one of the prefabs with the correct values and wipe the rest. And I think the other times that it's happened I didn't change any names (though I'm not 100% sure about that, but anyway I'm always very careful to use "FormerlySerializedAs" in these situations).

    The only thing that could be happening in this sense is that Unity stores the old names "somewhere" even after correctly changing them with "FormerlySerializedAs", and then at some point it tries to restore the serialized data with those wrong names, but I wouldn't actually bet too much on that.
     
  4. Trisibo

    Trisibo

    Joined:
    Nov 1, 2010
    Posts:
    233
    Holy sheep, I found the issue! I take back what I said, it's actually a problem when changing variable names, even when using "FormerlySerializedAs": if you make a build with the old names and then change them and apply "FormerlySerializedAs", go to the editor so the new names are applied, and go back and remove "FormerlySerializedAs", everything will work fine in the editor. But as soon as you make a new build the variables will be wiped (except it won't wipe them in some objects, for unknown reasons). If you don't remove "FormerlySerializedAs" before making the build, everything will be fine.

    I guess the serialization system maintains data separately for the editor and the other platforms, so when you make a build the serialized data for that platform, which doesn't have the new names, will be used.

    I consider this a bug, I'll prepare a repro project to send to Unity.
     
  5. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,199
    So the updated variable names are not automatically changed. You can see this if you use git or some other VC system, and open a scene where you've changed variable names in scripts. The yaml-based scene file (whatever.unity) doesn't update the names of the variables before something else has changed. So until Unity has actually done a change to the object, the variable will be stored with the old name, and the value will be written to the correct field on play.

    The lesson from this is: don't remove FormerlySerializedAs.
     
  6. belamessex

    belamessex

    Joined:
    May 12, 2015
    Posts:
    12
    So i know this is an old thread, but I really can't find any solutions to my very similar problem. I modified the standard assets third person control script and changed the gravity modifier variable from a serialized field to public. Ever since, my main character drops like a ton of bricks. I debugged the y velocity and gravity modifier and found that she drops faster in fullscreen mode, even though gravity modifier is unchanged according to debug.Log. When I click on the instance clone in the hierarchy during playmode, everything does what I expect it to, but gets buggy again once I deselect the clone. Have you found a solution? This really sucks.
     
  7. Trisibo

    Trisibo

    Joined:
    Nov 1, 2010
    Posts:
    233
    @belamessex Unity already fixed the issue I had, but anyway your problem seems to be completely unrelated. Did you modify something else in the script apart from setting the variable to public? I'd say the problem is something else.
     
  8. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Does the problem go away if you changed it back to a serialized field?