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.
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
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.
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): [System.Serializable] public class StatesVar { protected ChaseToNearPlayer _chaseToNearPlayer = new ChaseToNearPlayer(); protected ShortAttack _shortAttack = new ShortAttack(); protected Evade _evade = new Evade(); protected AirAttack _airAttack = new AirAttack(); protected Wander _wander = new Wander(); protected WanderByTime _wanderByTime = new WanderByTime(); protected WaitToPlayerReady _waitToPlayerReady = new WaitToPlayerReady(); public ChaseToNearPlayer chaseToNearPlayer { get => _chaseToNearPlayer; } public ShortAttack shortAttack { get => _shortAttack; } public Evade evade { get => _evade; } public AirAttack airAttack { get => _airAttack; } public Wander wander { get => _wander; } public WanderByTime wanderByTime { get => _wanderByTime; } public WaitToPlayerReady waitToPlayerReady { get => _waitToPlayerReady; } } if I do the code in this way , then unity crash in release build: Code (CSharp): [System.Serializable] public class StatesVar { public ChaseToNearPlayer _chaseToNearPlayer = new ChaseToNearPlayer(); public ShortAttack _shortAttack = new ShortAttack(); public Evade _evade = new Evade(); public AirAttack _airAttack = new AirAttack(); public Wander _wander = new Wander(); public WanderByTime _wanderByTime = new WanderByTime(); public WaitToPlayerReady _waitToPlayerReady = new WaitToPlayerReady(); } Its crazy, i dont understood why unity was crashing. I have commented all code until unity has worked.
Code (CSharp): [System.Serializable] public abstract class BaseState<T> { public UnityEvent onBegin; public UnityEvent onFinish; } [System.Serializable] public class ChaseToNearPlayer : BaseState<BasicIAMgr> { } [System.Serializable] public class Wander : ChaseToNearPlayer { } 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.
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
"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): [System.Serializable] public class StateVars { [SerializeField] protected ChaseToNearPlayer _chaseToNearPlayer = new ChaseToNearPlayer(); [SerializeField] protected ShortAttack _shortAttack = new ShortAttack(); [SerializeField] protected Evade _evade = new Evade(); [SerializeField] protected AirAttack _airAttack = new AirAttack(); [SerializeField] protected Wander _wander = new Wander(); [SerializeField] protected WanderByTime _wanderByTime = new WanderByTime(); [SerializeField] protected WaitToPlayerReady _waitToPlayerReady = new WaitToPlayerReady(); public ChaseToNearPlayer chaseToNearPlayer { get => _chaseToNearPlayer; } public ShortAttack shortAttack { get => _shortAttack; } public Evade evade { get => _evade; } public AirAttack airAttack { get => _airAttack; } public Wander wander { get => _wander; } public WanderByTime wanderByTime { get => _wanderByTime; } public WaitToPlayerReady waitToPlayerReady { get => _waitToPlayerReady; } }
It might be a bug in the serialization. A colleague of mine managed to reproduce it and create a bug report: https://fogbugz.unity3d.com/f/cases/1328667/
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.
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....