Skip to content

Conversation

nnra6864
Copy link
Contributor

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 #21

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
@nnra6864
Copy link
Contributor Author

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.

@hasanbayatme
Copy link
Member

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.

@hasanbayatme
Copy link
Member

This could in turn be related to how ExecuteAlways is handled internally as well, either way I'm merging it in.

@hasanbayatme hasanbayatme merged commit 9184522 into UnityCommunity:main Sep 15, 2025
@nnra6864
Copy link
Contributor Author

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.

@hasanbayatme
Copy link
Member

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.

@nnra6864
Copy link
Contributor Author

Ok, so here is sort of a step by step guide:

  1. Have a MonoSingleton, lets call it Singleton, that holds an object, lets use Player as an example:
        [SerializeField] private Transform _player;
        public static Transform Player => Instance._player ??= GameObject.FindWithTag("Player")?.transform;
  1. 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;
  1. Make sure that Project Settings -> Editor -> When entering Play Mode is set to Do not reload Domain or Scene
image
  1. 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 6000.3.0a5
Linux(in case that's in any way relevant)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Issues with usage in editor
2 participants