Search Unity

[Fixed] Scene Load - Inspector has reference, but code says null

Discussion in 'Editor & General Support' started by kcup024, Mar 10, 2019.

  1. kcup024

    kcup024

    Joined:
    Nov 25, 2018
    Posts:
    3
    Hi,

    I don't want to waste anyone's time so:

    MainLevel scene:

    Got a MatchDirector empty gameObject with my class in it,
    CameraSystem is a Camera with a CameraFollower.cs script.

    upload_2019-3-10_13-54-4.png

    CameraFollower.cs:

    Code (CSharp):
    1.  
    2. [SerializeField]
    3. private MatchDirector director;
    and (that OnNotify is called essentially by MatchDirector itself in its first Update() frame)

    Code (CSharp):
    1.  
    2. public void OnNotify(OBS_MSG_TYPE message)
    3. {
    4.         if (message == OBS_MSG_TYPE.AWAIT_ROLL)
    5.         {
    6.             Follow(director.GetTurnDirector().GetCurrentPlayer()
    7.             .GetCharacter().GetSelf());
    8.         }
    9. }
    upload_2019-3-10_13-55-52.png

    Everything works perfectly fine if I run the scene in editor.


    If I load the scene from my Main Menu:
    director is now null both in the function and when i hover over the SerializeField property. Other values in CameraFollower set in the inspector are fine (a Vector3 and float).
    upload_2019-3-10_13-57-5.png


    But:
    upload_2019-3-10_13-59-55.png
    the reference is still visible in the inspector as if it was completely valid.

    A couple of notes:
    • I checked and after pressing MatchDirector in the inspector it takes me to the right object in the hierarchy
    • I don't remove/destroy MatchDirector or change the director variable at any point in my code. I use Unity's suggested approach to load the scene:
    Code (CSharp):
    1.  
    2. IEnumerator LoadAsync()     {
    3.        AsyncOperation asyncLoad = SceneManager.LoadSceneAsync(
    4.       GameParameters.GetCurrentSceneTarget()
    5.       );
    6.        while (!asyncLoad.isDone)
    7.        {
    8.            yield return null;
    9.        }
    10. }
    • As I said works perfectly fine if I just run the scene directly
    • I don't do anything differently when loading the scene from Menu. I set a List<Player> in a static class and that's what's used by MatchDirector during initialisation (if I run the scene directly I enable a Test class that sets up said List)
    • I've tried putting [System.Serializable] above class name in MatchDirector.cs
    • It looks like other references work (e.g. in the same callstack, MatchDirector properly references the camera that's having issues, both in inspector and in code).

    I've tried searching for hours with no luck so I thought it might be worth creating a post.
    Any help appreciated.

    Thanks
     

    Attached Files:

  2. kcup024

    kcup024

    Joined:
    Nov 25, 2018
    Posts:
    3
    It seems like the issue was that menu camera also had CameraFollower on it (with a null reference because I didn't need the following there). That camera registered itself to the ObserverManager (which is a static class) and because of that, the ObserverManager tried to call the Notify method on both cameras - the proper one and the one from Main Menu.