@@ -53,14 +53,7 @@ internal static AsyncCoroutineRunner Instance
5353 {
5454 if ( instance == null )
5555 {
56- AsyncCoroutineRunner [ ] instances = FindObjectsOfType < AsyncCoroutineRunner > ( ) ;
57- Debug . Assert ( instances . Length <= 1 , "[AsyncCoroutineRunner] There should only be one AsyncCoroutineRunner in the scene." ) ;
58- instance = instances . Length == 1 ? instances [ 0 ] : null ;
59- if ( instance != null && ! instance . enabled )
60- {
61- Debug . LogWarning ( "[AsyncCoroutineRunner] Found a disabled AsyncCoroutineRunner component. Enabling the component." ) ;
62- instance . enabled = true ;
63- }
56+ instance = FindObjectOfType < AsyncCoroutineRunner > ( ) ;
6457 }
6558
6659 // FindObjectOfType() only search for objects attached to active GameObjects. The FindObjectOfType(bool includeInactive) variant is not available to Unity 2019.4 and earlier so cannot be used.
@@ -78,19 +71,6 @@ internal static AsyncCoroutineRunner Instance
7871 Debug . Log ( "[AsyncCoroutineRunner] Found a \" AsyncCoroutineRunner\" GameObject but didn't have the AsyncCoroutineRunner component attached. Attaching the script." ) ;
7972 instance = instanceGameObject . AddComponent < AsyncCoroutineRunner > ( ) ;
8073 }
81- else
82- {
83- if ( ! instance . enabled )
84- {
85- Debug . LogWarning ( "[AsyncCoroutineRunner] Found a disabled AsyncCoroutineRunner component. Enabling the component." ) ;
86- instance . enabled = true ;
87- }
88- if ( ! instanceGameObject . activeSelf )
89- {
90- Debug . LogWarning ( "[AsyncCoroutineRunner] Found an AsyncCoroutineRunner attached to an inactive GameObject. Setting the GameObject active." ) ;
91- instanceGameObject . SetActive ( true ) ;
92- }
93- }
9474 }
9575 }
9676
@@ -99,6 +79,19 @@ internal static AsyncCoroutineRunner Instance
9979 Debug . Log ( "[AsyncCoroutineRunner] There is no AsyncCoroutineRunner in the scene. Adding a GameObject with AsyncCoroutineRunner attached at the root of the scene." ) ;
10080 instance = new GameObject ( "AsyncCoroutineRunner" ) . AddComponent < AsyncCoroutineRunner > ( ) ;
10181 }
82+ else if ( ! instance . isActiveAndEnabled )
83+ {
84+ if ( ! instance . enabled )
85+ {
86+ Debug . LogWarning ( "[AsyncCoroutineRunner] Found a disabled AsyncCoroutineRunner component. Enabling the component." ) ;
87+ instance . enabled = true ;
88+ }
89+ if ( ! instance . gameObject . activeSelf )
90+ {
91+ Debug . LogWarning ( "[AsyncCoroutineRunner] Found an AsyncCoroutineRunner attached to an inactive GameObject. Setting the GameObject active." ) ;
92+ instance . gameObject . SetActive ( true ) ;
93+ }
94+ }
10295
10396 instance . gameObject . hideFlags = HideFlags . None ;
10497
@@ -130,7 +123,12 @@ internal static void Post(Action task)
130123
131124 private void Update ( )
132125 {
133- Debug . Assert ( Instance == this , "[AsyncCoroutineRunner] There should only be one AsyncCoroutineRunner in the scene." ) ;
126+ if ( Instance != this )
127+ {
128+ Debug . Log ( "[AsyncCoroutineRunner] Multiple active AsyncCoroutineRunners is present in the scene. Disabling duplicate ones." ) ;
129+ enabled = false ;
130+ return ;
131+ }
134132 isInstanceRunning = true ;
135133
136134 int actionCount ;
@@ -163,8 +161,15 @@ private void OnDisable()
163161
164162 private void OnEnable ( )
165163 {
166- Debug . Assert ( Instance == this , "[AsyncCoroutineRunner] There should only be one AsyncCoroutineRunner in the scene." ) ;
167- isInstanceRunning = true ;
164+ if ( Instance != this )
165+ {
166+ Debug . Log ( "[AsyncCoroutineRunner] Multiple active AsyncCoroutineRunners is present in the scene. Disabling duplicate ones." ) ;
167+ enabled = false ;
168+ }
169+ else
170+ {
171+ isInstanceRunning = true ;
172+ }
168173 }
169174 }
170175}
0 commit comments