-
Notifications
You must be signed in to change notification settings - Fork 69
Fixed the existing instance being deleted in Awake #22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed the existing instance being deleted in Awake #22
Conversation
Replaced `else` with `else if (instance != this)` as a condition for the instance to be deleted in Awake. This fixes a really weird bug where if singleton was used in the editor and then again in play mode, it would delete the original instance and create a new one. Closes UnityCommunity#21
I would highly appreciate somebody looking into this a bit deeper before merging.
From my testing, it didn't introduce any issues, however, there could be edge cases where this is unwanted behavior.
I am guessing it is due to the marshaling that Unity does for its objects, and MonoBehaviour nonetheless is a UnityEngine.Object, therefore it is not null in C#'s context as it is still living as a wrapper for the underlying C++ object until it is garbage collected, but it is not this either, so adding this check shouldn't theoretically make a difference, I am wondering whether in your case the issue might have been resolved using another solution or possibly another trial or fix? was it resolved in conjunction with #23 PR?
Unity has a custom implementation for for == equality check that does this native checking for the objects.
That == is already happening for instance == null, I assume there is an interoperability issue going on, most possibly when switching from Edit to Play mode.
This could in turn be related to how ExecuteAlways is handled internally as well, either way I'm merging it in.
am wondering whether in your case the issue might have been resolved using another solution or possibly another trial or fix?
I am positive it's the PR I made.
As soon as I use just else, the issue comes back.
was it resolved in conjunction with #23 PR?
I initially tried utilizing the execution order to fix the issue I was experiencing, that didn't help tho.
I just deleted the execution order attribute and left the else if alone and the issue stays fixed, I don't think execution order has anything to do with the fix in my case.
I tried to debug this before I came up with the solution, but I am sadly not skilled enough to figure it out.
Best I can do is help you setup a consistent way of reproducing this issue on your end, if you are up to of course.
Interesting, thank you for the elaboration.
Best I can do is help you setup a consistent way of reproducing this issue on your end
Sure, I'm interested to see how this occurs and whether this is an issue related to specific Unity versions or not.
Ok, so here is sort of a step by step guide:
- Have a MonoSingleton, lets call it
Singleton, that holds an object, lets usePlayeras an example:
[SerializeField] private Transform _player; public static Transform Player => Instance._player ??= GameObject.FindWithTag("Player")?.transform;
- Have a script with
[ExecuteAlways]that would use this object in Update:
private static Transform Player => Singleton.Player; private void Update() => transform.position = Player.position;
- Make sure that
Project Settings -> Editor -> When entering Play Modeis set toDo not reload Domain or Scene
- Enter the playmode
Some of the code above might seem weird, I tried to make it as close as possible to what I have on my end. Some of it may also not be functional, I mostly wrote it on github so no way to test.
Unity 600030a5
Linux(in case that's in any way relevant)
Replaced
elsewithelse if (instance != this)as a condition for the instance to be deleted in Awake.This fixes a really weird bug where if singleton was used in the editor and then again in play mode, it would delete the original instance and create a new one.
Closes #21