Search Unity

Getting Object reference not set even though it is

Discussion in '2D' started by mpodlesny, Oct 21, 2020.

  1. mpodlesny

    mpodlesny

    Joined:
    Jun 29, 2020
    Posts:
    43
    SUMMARY OF THE ISSUE
    I have a Game Object and attached to that is a script called "GameSession"

    When I run the game, the game works fine. When the player loses all 3 lives, the game goes to the next scene which is the "End Credits" scene, where the player's final score is displayed and there is a "Play Again" button.

    When I click the "Play Again" button, I go back to the original scene, and now I get the errors.

    When the player dies this code is executed:

    Code (CSharp):
    1.         if (numberOfLives <= 0)
    2.         {
    3.           SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
    4.  
    5. }
    So far, pretty straight forward. I move to the next scene.

    On the next scene I have a game object with a script attached that has the following code:

    Code (CSharp):
    1. public class EndCredits : MonoBehaviour
    2. {
    3.     private GameSession gamePlay;
    4.     [SerializeField] TextMeshProUGUI playerScoreText;
    5.  
    6.     // Start is called before the first frame update
    7.     void Start()
    8.     {
    9.         gamePlay = FindObjectOfType<GameSession>();
    10.         playerScoreText.text = "YOUR SCORE: " + gamePlay.playerScore.ToString() + Environment.NewLine + "YOUR LEVEL: " + gamePlay.playerLevel.ToString();
    11.         Destroy(gamePlay);
    12.     }
    13.  
    14. }
    Again pretty straight forward. I don't destroy the previous game session until I get the values I need to display to the user. Once I set the text, I destroy the game session.

    The "Play Again" button runs this code when it is clicked:
    Code (CSharp):
    1.     public void LoadMainMenu()
    2.     {
    3.         SceneManager.LoadScene(0);
    4.     }
    Again pretty straight forward.

    The error occurs in the player script when on these lines:
    Code (CSharp):
    1.     private void OnTriggerEnter2D(Collider2D collision)
    2.     {
    3.         if (collision.tag == "Right Shredder")
    4.         {
    5.             //show finish line bob
    6.             gameSession.GetComponent<GameSession>().ShowFinishLineBob();
    7.         }
    8.         else
    9.         {
    10.             gameSession.GetComponent<GameSession>().ProcessPlayerDeath();          
    11.         }
    12.        
    13.     }
    Specifically on the "gameSession" which is initialized in the start method of this script as follows:
    Code (CSharp):
    1.         gameSession = GameObject.Find("GameSession");
    What's weird, is, this code works fine when you run the game. It only breaks when you try to start a new game via the "Play Again" button.

    Hope this helps. If you need any further info, let me know.
     
  2. mpodlesny

    mpodlesny

    Joined:
    Jun 29, 2020
    Posts:
    43
    Ok I found my issue. I wanted to post it in case anyone else had the same. I changed the "OnTrigger2d" code to this:
    Code (CSharp):
    1.     private void OnTriggerEnter2D(Collider2D collision)
    2.     {
    3.         if (collision.tag == "Right Shredder")
    4.         {
    5.             FindObjectOfType<GameSession>().ShowFinishLineBob();
    6.         }
    7.         else
    8.         {
    9.             FindObjectOfType<GameSession>().ProcessPlayerDeath();
    10.  
    11.         }
    12.        
    13.     }
    This works for me.