Search Unity

Question Script stops execution without any errors or warnings

Discussion in 'Scripting' started by jorge89, Oct 18, 2020.

  1. jorge89

    jorge89

    Joined:
    May 28, 2015
    Posts:
    13
    Here are the two scripts involved in this issue. When GoToHomePanel() is called "1" prints in the console, but when homePanel.SlideInToLeft() is called nothing else happens and there are no errors or warnings. I've looked at all the game objects in the editor and they are all enabled.

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using Extensions;
    3. using Firebase.Auth;
    4. using UnityEngine;
    5.  
    6. public class PanelController : MonoBehaviour
    7. {
    8.     [SerializeField] private List<PanelAnimationController> panels;
    9.     [SerializeField] private PanelAnimationController signInPanel;
    10.     [SerializeField] private PanelAnimationController homePanel;
    11.  
    12.     private void Start()
    13.     {
    14.         if (FirebaseAuth.DefaultInstance.CurrentUser == null)
    15.         {
    16.             Debug.Log("not in");
    17.             signInPanel.FirstPanel();
    18.             panels.Add(signInPanel);
    19.             signInPanel.rectTransform.SetLeft(0f);
    20.             signInPanel.rectTransform.SetRight(0f);
    21.             homePanel.rectTransform.SetLeft(1080f);
    22.             homePanel.rectTransform.SetRight(-1080f);
    23.         }
    24.         else
    25.         {
    26.             Debug.Log("in");
    27.             panels.Add(homePanel);
    28.             homePanel.FirstPanel();
    29.             signInPanel.rectTransform.SetLeft(-1080f);
    30.             signInPanel.rectTransform.SetRight(1080f);
    31.             homePanel.rectTransform.SetLeft(0f);
    32.             homePanel.rectTransform.SetRight(0f);
    33.         }
    34.     }
    35.  
    36.     public void GoToNextPanel(PanelAnimationController nextPanel)
    37.     {
    38.         var currentPanel = panels[panels.Count - 1];
    39.         currentPanel.SlideOutToLeft();
    40.         panels.Add(nextPanel);
    41.         nextPanel.SlideInToLeft();
    42.     }
    43.  
    44.     public void GoToPreviousPanel()
    45.     {
    46.         var currentPanelIndex = panels.Count - 1;
    47.         panels[currentPanelIndex].SlideOutToRight();
    48.         panels.RemoveAt(currentPanelIndex);
    49.         panels[panels.Count - 1].SlideInToRight();
    50.     }
    51.  
    52.     public void GoToSignInPanel()
    53.     {
    54.         panels[panels.Count - 1].SlideOutToRight();
    55.         panels.Clear();
    56.         panels.Add(signInPanel);
    57.         signInPanel.SlideInToRight();
    58.     }
    59.  
    60.     public void GoToHomePanel()
    61.     {
    62.         panels.Clear();
    63.         panels.Add(homePanel);
    64.         Debug.Log(1);
    65.         homePanel.SlideInToLeft();
    66.         Debug.Log(2);
    67.     }
    68. }
    69.  
    and

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PanelAnimationController : MonoBehaviour
    4. {
    5.     [SerializeField] private Animator mAnimator;
    6.     private static readonly int SlideOutLeft = Animator.StringToHash("slideOutLeft");
    7.     private static readonly int SlideOutRight = Animator.StringToHash("slideOutRight");
    8.     private static readonly int SlideInLeft = Animator.StringToHash("slideInLeft");
    9.     private static readonly int SlideInRight = Animator.StringToHash("slideInRight");
    10.     private static readonly int First = Animator.StringToHash("first");
    11.  
    12.     public RectTransform rectTransform;
    13.  
    14.     private void Awake()
    15.     {
    16.         mAnimator = gameObject.GetComponent<Animator>();
    17.         rectTransform = gameObject.GetComponent<RectTransform>();
    18.     }
    19.  
    20.     public void FirstPanel()
    21.     {
    22.         mAnimator.SetTrigger(First);
    23.     }
    24.  
    25.     public void SlideOutToLeft()
    26.     {
    27.         Debug.Log(gameObject.name + ": SlideOutToLeft()");
    28.         mAnimator.SetTrigger(SlideOutLeft);
    29.     }
    30.  
    31.     public void SlideOutToRight()
    32.     {
    33.         mAnimator.SetTrigger(SlideOutRight);
    34.     }
    35.  
    36.     public void SlideInToLeft()
    37.     {
    38.         Debug.Log(gameObject.name + ": SlideInToLeft()");
    39.         mAnimator.SetTrigger(SlideInLeft);
    40.     }
    41.  
    42.     public void SlideInToRight()
    43.     {
    44.         mAnimator.SetTrigger(SlideInRight);
    45.     }
    46. }
     
  2. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    677
    That does seem odd. After Line 64 in
    PanelController
    , add a
    print(homePanel);
    and tell us what it does.
     
  3. jorge89

    jorge89

    Joined:
    May 28, 2015
    Posts:
    13
    Nothing happens. I've made sure that the game object with that controller is active and that the homePanel variable is pointing to the right place
     
  4. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    677
    When you say "nothing happens," do you mean that it prints "1" to the console and then nothing else?
     
  5. jorge89

    jorge89

    Joined:
    May 28, 2015
    Posts:
    13
    yes
     
  6. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    677
    That's quite strange. Are you sure you have saved the change and recompiled the code? Let's change
    Debug.Log(1);
    to
    Debug.Log("One");
    , to be sure.

    Also, it appears you have added some extension methods to the RectTransform class. Probably harmless, but could you post those, to be sure?
     
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,011
    There are many things not clear. First of all from where do you actually call "GoToHomePanel"? By any chance from inside another thread or a coroutine? Next thing is use meaningful log messages and always add a context parameter to Debug.Log. That way you can identify the actual object when you click on the log message in the console. So do

    Code (CSharp):
    1. Debug.Log("Before SlideInToLeft", gameObject);
    2. homePanel.SlideInToLeft();
    3. Debug.Log("After SlideInToLeft", gameObject);
    Like others have said, what exactly do you mean by "nothing happens"? Do you mean Unity happily keeps running and is responsive but you don't see those other logs in the console?

    My guess is that either:

    • You run the code on a seperate thread which throws an exception which is silently swallowed.
    • You may haven't saved your scripts properly or for some reason they haven't been compiled so your latest changes did not apply.
    You may want to add more Debug.Logs with all relevant context objects so you can figure out if the references you deal with are the right objects.
     
  8. jorge89

    jorge89

    Joined:
    May 28, 2015
    Posts:
    13
    Yeah, this time it printed "One" instead and still nothing after that
     
  9. jorge89

    jorge89

    Joined:
    May 28, 2015
    Posts:
    13
    This is the code that calls that function
    Code (CSharp):
    1. using Firebase.Auth;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class SignIn : MonoBehaviour
    6. {
    7.     private FirebaseAuth _auth;
    8.     private FirebaseUser _user;
    9.     [SerializeField] private InputField emailField;
    10.     [SerializeField] private InputField passwordField;
    11.     [SerializeField] private PanelController panelController;
    12.    
    13.     // Start is called before the first frame update
    14.     private void Start()
    15.     {
    16.         _auth = FirebaseAuth.DefaultInstance;
    17.     }
    18.  
    19.     public void SignInWithEmailAndPasswordAsync()
    20.     {
    21.         Debug.LogFormat("email: {0}, password {1}", emailField.text, passwordField.text);
    22.         _auth.SignInWithEmailAndPasswordAsync(emailField.text, passwordField.text).ContinueWith(task => {
    23.             if (task.IsCanceled) {
    24.                 Debug.LogError("SignInWithEmailAndPasswordAsync was canceled.");
    25.                 return;
    26.             }
    27.             if (task.IsFaulted) {
    28.                 Debug.LogError("SignInWithEmailAndPasswordAsync encountered an error: " + task.Exception);
    29.                 return;
    30.             }
    31.  
    32.             _user = task.Result;
    33.             Debug.LogFormat("User signed in successfully: {0} ({1})", _user.DisplayName, _user.UserId);
    34.             panelController.GoToHomePanel();
    35.         });
    36.     }
    37. }
    I'm not familiar with the firebase auth package so I can't say if it's running on another thread.

    What I mean by "nothing happens" is that I don't see any more prints and the Unity keeps running.
     
  10. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    677
    Okay, we're going to have to take this in very tiny steps. Please add this as a new Line 65:

    print("Another One");


    Run that and please post your full code again. That way, we'll see if it truly is dying at
    print(homePanel);
    . If it is, then something horrific has happened inside your
    homePanel
    object.
     
  11. jorge89

    jorge89

    Joined:
    May 28, 2015
    Posts:
    13
    This is wha that script looks like now and it only printed "One"

    Code (CSharp):
    1. using System.Collections.Generic;
    2. using Extensions;
    3. using Firebase.Auth;
    4. using UnityEngine;
    5.  
    6. public class PanelController : MonoBehaviour
    7. {
    8.     [SerializeField] private List<PanelAnimationController> panels;
    9.     [SerializeField] private PanelAnimationController signInPanel;
    10.     [SerializeField] private PanelAnimationController homePanel;
    11.  
    12.     private void Start()
    13.     {
    14.         if (FirebaseAuth.DefaultInstance.CurrentUser == null)
    15.         {
    16.             Debug.Log("not in");
    17.             signInPanel.FirstPanel();
    18.             panels.Add(signInPanel);
    19.             signInPanel.rectTransform.SetLeft(0f);
    20.             signInPanel.rectTransform.SetRight(0f);
    21.             homePanel.rectTransform.SetLeft(1080f);
    22.             homePanel.rectTransform.SetRight(-1080f);
    23.         }
    24.         else
    25.         {
    26.             Debug.Log("in");
    27.             panels.Add(homePanel);
    28.             homePanel.FirstPanel();
    29.             signInPanel.rectTransform.SetLeft(-1080f);
    30.             signInPanel.rectTransform.SetRight(1080f);
    31.             homePanel.rectTransform.SetLeft(0f);
    32.             homePanel.rectTransform.SetRight(0f);
    33.         }
    34.     }
    35.  
    36.     public void GoToNextPanel(PanelAnimationController nextPanel)
    37.     {
    38.         var currentPanel = panels[panels.Count - 1];
    39.         currentPanel.SlideOutToLeft();
    40.         panels.Add(nextPanel);
    41.         nextPanel.SlideInToLeft();
    42.     }
    43.  
    44.     public void GoToPreviousPanel()
    45.     {
    46.         var currentPanelIndex = panels.Count - 1;
    47.         panels[currentPanelIndex].SlideOutToRight();
    48.         panels.RemoveAt(currentPanelIndex);
    49.         panels[panels.Count - 1].SlideInToRight();
    50.     }
    51.  
    52.     public void GoToSignInPanel()
    53.     {
    54.         panels[panels.Count - 1].SlideOutToRight();
    55.         panels.Clear();
    56.         panels.Add(signInPanel);
    57.         signInPanel.SlideInToRight();
    58.     }
    59.  
    60.     public void GoToHomePanel()
    61.     {
    62.         panels.Clear();
    63.         panels.Add(homePanel);
    64.         Debug.Log("One");
    65.         Debug.Log(homePanel);
    66.         Debug.Log("Another one");
    67.         homePanel.SlideInToLeft();
    68.         Debug.Log(2);
    69.     }
    70. }
     
  12. jorge89

    jorge89

    Joined:
    May 28, 2015
    Posts:
    13
    I figured it out. I started looking more into Firebase authentication and changed the SignIn script to this
    Code (CSharp):
    1. using Firebase.Auth;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. public class SignIn : MonoBehaviour
    6. {
    7.     private FirebaseAuth _auth;
    8.     private FirebaseUser _user;
    9.     [SerializeField] private InputField emailField;
    10.     [SerializeField] private InputField passwordField;
    11.     [SerializeField] private PanelController panelController;
    12.    
    13.     // Start is called before the first frame update
    14.     private void Start()
    15.     {
    16.         _auth = FirebaseAuth.DefaultInstance;
    17.         _auth.StateChanged += AuthStateChanged;
    18.         AuthStateChanged(this, null);
    19.     }
    20.  
    21.     public void SignInWithEmailAndPasswordAsync()
    22.     {
    23.         Debug.LogFormat("email: {0}, password {1}", emailField.text, passwordField.text);
    24.         _auth.SignInWithEmailAndPasswordAsync(emailField.text, passwordField.text).ContinueWith(task => {
    25.             if (task.IsCanceled) {
    26.                 Debug.LogError("SignInWithEmailAndPasswordAsync was canceled.");
    27.                 return;
    28.             }
    29.             if (task.IsFaulted) {
    30.                 Debug.LogError("SignInWithEmailAndPasswordAsync encountered an error: " + task.Exception);
    31.                 return;
    32.             }
    33.  
    34.             // _user = task.Result;
    35.             Debug.LogFormat("User signed in successfully: {0} ({1})", _user.DisplayName, _user.UserId);
    36.             // panelController.GoToHomePanel();
    37.         });
    38.     }
    39.  
    40.     public void AuthStateChanged(object sender, System.EventArgs eventArgs) {
    41.         if (_auth.CurrentUser != _user) {
    42.             bool signedIn = _user != _auth.CurrentUser && _auth.CurrentUser != null;
    43.            
    44.             if (!signedIn && _user != null) {
    45.                 Debug.LogFormat("Signed out " + _user.UserId);
    46.             }
    47.            
    48.             _user = _auth.CurrentUser;
    49.            
    50.             if (signedIn) {
    51.                 Debug.LogFormat(gameObject.name + " Signed in " + _user.UserId);
    52.                 Debug.Log(panelController);
    53.                 panelController.GoToHomePanel();
    54.             }
    55.         }
    56.     }
    57. }
    I found out that another object also had the SignIn script attached without a panelController assigned. After removing the script from this button everything started working.

    Thanks for all the help.
     
  13. Stevens-R-Miller

    Stevens-R-Miller

    Joined:
    Oct 20, 2017
    Posts:
    677
    Glad you got it working. Nothing more frustrating than a program that just silently dies.
     
  14. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    4,011
    So you actually executed your method on another thread. That's why the exception didn't show up Exceptions thrown in your own thread do not show up in the console. You either have to use try catch and show the exception yourself, or you could implement a handler for unhandled exception for your appdomain. For more information see this thread.