2 errors for Camera Controller scene transition. 2D Pixel RPG

I have checked the code several times and mine is exactly as the instructor’s code, but when I go from scene 1 to scene 2 no issues. However, when I go back to scene 1 from scene 2, I receive two errors as follow:

The first error only occurs if I am in the canopy when transitioning from scene 2 to scene 1.

Coroutine couldn’t be started because the game object ‘Canopy’ is inactive!
UnityEngine.MonoBehaviour:StartCoroutine (System.Collections.IEnumerator)
TransparentDetection:OnTriggerExit2D (UnityEngine.Collider2D) (at Assets/Scripts/Misc/TransparentDetection.cs:47)

NullReferenceException: Object reference not set to an instance of an object
PlayerController.OnDisable () (at Assets/Scripts/Player/PlayerController.cs:48)
UnityEngine.Object:Destroy(Object)
Singleton`1:Awake() (at Assets/Scripts/Scene Management/Singleton.cs:14)
PlayerController:Awake() (at Assets/Scripts/Player/PlayerController.cs:26)

This first one is tricky, although usually the error occurs in the middle of the FadeRoutine. We try to prevent it by checking the TileMap first for validity before starting the coroutine, but the Physics system still has the collision in it’s buffer.

The error itself shouldn’t cause any real problems, because as the scene changes, everything involve is destroyed and the game should keep right on going.

One preventative is to ensure that no portals are obscured by the canopy.

I tend to put this in TransparentDetection as well, though it won’t stop this particular error, only the one that fires while we’re IN the coroutine:

void OnDisable()
{
   StopAllCoroutines();
}

This one is a bit of an accidental race condition issue. Here’s what’s happening:
When base.Awake() is called on the new PlayerController in the scene, the Instance is detected and the new PlayerController is immediately destroyed. When a MonoBehaviour is Destroyed, OnDisable() is called first…

    private void OnDisable() {
        playerControls.Disable();
    }

But we didn’t create the controls yet because the new PlayerController is being destroyed first. We don’t want to create the controls.

All we need to do in OnDisable() is to check to make sure that the controls exist before disabling them. Since the PlayerControls are not a MonoBehaviour, you can disable them using null coalescence.

    private void OnDisable() {
        playerControls?.Disable();
    }

That little ? before the .Disable() is a short hand for

if(playerControls!=null)
{
    playerControls.Disable();
}

which is what we must do if you’re testing a MonoBehaviour.

1 Like

Thank you. For the first error, I did try that partial solution and as noted, it is only a partial solution, so I removed the canopy from around any transition point.

For the second error, I note that I kept some code in that Stephen had only mentioned as might being needed later but has not been introduced as being needed yet. I changed as you suggested, and it works well. But since the code has not been formally required, I commented it out and that works too.

Again, thanks for the very quick reply.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms