Search Unity

Missing Variables on Release

Discussion in 'Prefabs' started by jviruss, Apr 11, 2021.

  1. jviruss

    jviruss

    Joined:
    Oct 4, 2016
    Posts:
    48
    I have found a problem in release. Public variables are reseted to default value( int to 0, objects to null) when i do an prefab instance.
    In debug mode or in editor mode the variables work correctly.If I put variables with protected modifier all works fine.

    How I can solve this problem, all proyect is broken in release mode.


    Unity version: I have changing the proyect between 2019.4.23 and 2021.
     
  2. pietrodenicola

    pietrodenicola

    Unity Technologies

    Joined:
    Dec 8, 2020
    Posts:
    43
    Hi.

    Was it working as expected on 2019.4.23 ?
     
  3. jviruss

    jviruss

    Joined:
    Oct 4, 2016
    Posts:
    48
    No, neither in 2019.4.24f.I have solved setting some variables with protected modifier, but i need change them to public. I have tested different versions of unity :

    2019.3.14 CRASH
    2019.4.22 CRASH
    2019.4.23 CRASH
    2019.4.24 CRASH
    2020.3.3f1 CRASH
    2021.1.2.f1 CRASH
     
    Last edited: Apr 12, 2021
  4. pietrodenicola

    pietrodenicola

    Unity Technologies

    Joined:
    Dec 8, 2020
    Posts:
    43
    What if the variable is private and the access is given via get and set methods?

    What if the initial values are given via event Awake? Look at this example: https://docs.unity3d.com/ScriptReference/MonoBehaviour.Start.html

    It's already interesting the fact that when defined as protected, the variable retains its value. And if you could provide a small portion of code, where you show how you define these variables and how you access to them, I could possibly tell more.
     
    Last edited: Apr 12, 2021
  5. jviruss

    jviruss

    Joined:
    Oct 4, 2016
    Posts:
    48
    What if the variable is private and the access is given view get and set methods?
    Works fine. Keep correctly the values.

    This is the variable declaration:

    Code (CSharp):
    1. [System.Serializable]
    2. public class StatesVar
    3. {
    4.     protected ChaseToNearPlayer _chaseToNearPlayer = new ChaseToNearPlayer();
    5.     protected ShortAttack _shortAttack = new ShortAttack();
    6.     protected Evade _evade = new Evade();
    7.     protected AirAttack _airAttack = new AirAttack();
    8.     protected Wander _wander = new Wander();
    9.     protected WanderByTime _wanderByTime = new WanderByTime();
    10.     protected WaitToPlayerReady _waitToPlayerReady = new WaitToPlayerReady();
    11.  
    12.     public ChaseToNearPlayer chaseToNearPlayer
    13.     {
    14.         get => _chaseToNearPlayer;
    15.     }
    16.     public ShortAttack shortAttack
    17.     {
    18.         get => _shortAttack;
    19.     }
    20.     public Evade evade
    21.     {
    22.         get => _evade;
    23.     }
    24.     public AirAttack airAttack
    25.     {
    26.         get => _airAttack;
    27.     }
    28.     public Wander wander
    29.     {
    30.         get => _wander;
    31.     }
    32.     public WanderByTime wanderByTime
    33.     {
    34.         get => _wanderByTime;
    35.     }
    36.     public WaitToPlayerReady waitToPlayerReady
    37.     {
    38.         get => _waitToPlayerReady;
    39.     }
    40.  
    41.  
    42. }
    43.  
    44.  

    if I do the code in this way , then unity crash in release build:

    Code (CSharp):
    1. [System.Serializable]
    2. public class StatesVar
    3. {
    4.     public ChaseToNearPlayer _chaseToNearPlayer = new ChaseToNearPlayer();
    5.     public ShortAttack _shortAttack = new ShortAttack();
    6.     public Evade _evade = new Evade();
    7.     public AirAttack _airAttack = new AirAttack();
    8.     public Wander _wander = new Wander();
    9.     public WanderByTime _wanderByTime = new WanderByTime();
    10.     public WaitToPlayerReady _waitToPlayerReady = new WaitToPlayerReady();
    11. }
    Its crazy, i dont understood why unity was crashing. I have commented all code until unity has worked.
     
  6. jviruss

    jviruss

    Joined:
    Oct 4, 2016
    Posts:
    48
    Code (CSharp):
    1. [System.Serializable]
    2. public abstract class BaseState<T>
    3. {
    4.     public UnityEvent onBegin;
    5.     public UnityEvent onFinish;
    6. }
    7.  
    8.  
    9.  
    10. [System.Serializable]
    11. public class ChaseToNearPlayer : BaseState<BasicIAMgr>
    12. {
    13.  
    14. }
    15.  
    16. [System.Serializable]
    17. public class Wander : ChaseToNearPlayer
    18. {
    19. }
    There are not initilizations in awake.

    If I put ChaseToNearPlayer as public works fine, but if I put wander as public unity crash only on release build.

    if you want we can do a parsec session and I show you the project, and I demonstrate the error, but I can not send the code.But is imposible to work with this bug, it is so much crazy, is important to be fixed. I have been working with unity engine for 10 years and never has me pass this.
     
    Last edited: Apr 12, 2021
  7. pietrodenicola

    pietrodenicola

    Unity Technologies

    Joined:
    Dec 8, 2020
    Posts:
    43
    I'd say: keep the "protected" version, just because it works. "Protected" and "private" also allow you to specify what of the object to expose to the outer world and what not. And it's generally good practice to keep protected or private all that is not necessary to be exposed.

    Do you have a call stack of the crash?

    If you can make a small project, with just the few elements necessary to reproduce the problem, we will be happy to have a look. Best if you can submit a bug report with that project attached.
    https://unity3d.com/unity/qa/bug-reporting
     
    Last edited: Apr 12, 2021
  8. jviruss

    jviruss

    Joined:
    Oct 4, 2016
    Posts:
    48
    "Protected" and "private" also allow you to specify what of the object to expose to the outer world and what not.

    This piece of code crash too. No posible to expose variables in editor.


    Code (CSharp):
    1. [System.Serializable]
    2. public class StateVars
    3. {
    4.     [SerializeField]
    5.     protected ChaseToNearPlayer _chaseToNearPlayer = new ChaseToNearPlayer();
    6.     [SerializeField]
    7.     protected ShortAttack _shortAttack = new ShortAttack();
    8.     [SerializeField]
    9.     protected Evade _evade = new Evade();
    10.     [SerializeField]
    11.     protected AirAttack _airAttack = new AirAttack();
    12.     [SerializeField]
    13.     protected Wander _wander = new Wander();
    14.     [SerializeField]
    15.     protected WanderByTime _wanderByTime = new WanderByTime();
    16.    [SerializeField]
    17.     protected WaitToPlayerReady _waitToPlayerReady = new WaitToPlayerReady();
    18.  
    19.     public ChaseToNearPlayer chaseToNearPlayer
    20.     {
    21.         get => _chaseToNearPlayer;
    22.     }
    23.     public ShortAttack shortAttack
    24.     {
    25.         get => _shortAttack;
    26.     }
    27.     public Evade evade
    28.     {
    29.         get => _evade;
    30.     }
    31.     public AirAttack airAttack
    32.     {
    33.         get => _airAttack;
    34.     }
    35.     public Wander wander
    36.     {
    37.         get => _wander;
    38.     }
    39.     public WanderByTime wanderByTime
    40.     {
    41.         get => _wanderByTime;
    42.     }
    43.     public WaitToPlayerReady waitToPlayerReady
    44.     {
    45.         get => _waitToPlayerReady;
    46.     }
    47.  
    48. }
    49.  
     
  9. pietrodenicola

    pietrodenicola

    Unity Technologies

    Joined:
    Dec 8, 2020
    Posts:
    43
  10. jviruss

    jviruss

    Joined:
    Oct 4, 2016
    Posts:
    48
    I have reproduced the crash into a minimal exaple. Import this example in a proyect.

    Build a release build, Unity will crash. Then open now the player.cs file, change FollowState var from public to protected, then unity will not crash. This bug exists in all unity versions, please fix it and report me the version with the fix.
     

    Attached Files:

    Last edited: Apr 13, 2021
  11. jviruss

    jviruss

    Joined:
    Oct 4, 2016
    Posts:
    48
    Some news about the crash @pietrodenicola ?this bug coexist with another one which consists in variables are reseted to default values in release build, objects to null and int to zero....
     
    Last edited: Apr 13, 2021