Forum begins after the advertisement:


[Part 5] Scene Transition Errors

Home Forums Video Game Tutorial Series Creating a Metroidvania in Unity [Part 5] Scene Transition Errors

Viewing 11 posts - 21 through 31 (of 31 total)
  • Author
    Posts
  • #19165
    Nirvik Rajbhandari
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    Sorry I didn’t see your post. On all scenes only the one player object appears.

    #19166
    Ser Apustaja
    Level 6
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    It sounds like you’re encountering issues with your player object not being properly instantiated or referenced during scene transitions, which is likely causing the NullReferenceException errors.

    Given that the error occurs in the Move() method, it’s possible that the Player_State or other related components are not being set up correctly when transitioning between scenes. Here are a few steps to troubleshoot and resolve the issue:

    1. Ensure Player State Initialization: Make sure that the Player_State_List component is properly assigned to the player object in every scene. If the player object is instantiated in a new scene, ensure that it has all the necessary components attached before any methods that rely on them are called.

    2. Check Scene Transition Logic: In your Scene_Transition script, ensure that the player object is fully initialized before calling any methods that depend on its state. You might want to delay the execution of the Walk_Into_New_Scene coroutine until after the scene has fully loaded.

    3. Debugging: Add debug logs in the Move() method to check if Player_State or any other critical variables are null when the method is called. For example:

      Debug.Log(Player_State); // Check if Player_State is null
    4. Singleton Pattern: Ensure that your singleton pattern for Player_Controller is functioning correctly. If the player object is destroyed and recreated during scene transitions, the Instance variable may not point to the correct object.

    5. Scene Management: If you’re using DontDestroyOnLoad for your player object, ensure that it is not being duplicated when transitioning to a new scene. You can check for existing instances in the Awake method of your Player_Controller script.

    6. Image Reference: In the image you provided, check if the player object is being instantiated correctly in the new scene. If it appears only in one scene, ensure that the scene transition logic is correctly handling the player object.

    If you continue to experience issues, could you clarify whether the player object is set to DontDestroyOnLoad and if you have any logic in place to prevent multiple instances of the player from being created? This information will help in diagnosing the problem further.

    #19167
    Nirvik Rajbhandari
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    the player object is not set to dontdestroyonload

    #19168
    Ser Apustaja
    Level 6
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    It seems that the issue arises because the player object is not set to DontDestroyOnLoad, which means it gets destroyed when transitioning between scenes. To fix this, ensure that you call DontDestroyOnLoad(gameObject); in the Awake method of your Player_Controller script.

    Here’s how it should look:

    private void Awake()
    {
        if (Instance != null && Instance != this)
        {
            Destroy(gameObject);
        }
        else
        {
            Instance = this;
            DontDestroyOnLoad(gameObject); // Ensure the player object persists across scenes
        }
    }

    Make sure this line is present so that the player object remains in the scene when transitioning. After making this change, test the scene transition again to see if the NullReferenceException errors persist. If they do, please provide any additional error messages or behaviors you observe.

    #19169
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::

    @nirvikrajbhandari send a copy of your project files over if you’re still unable to fix the issue.

    #19170
    #19171
    Ser Apustaja
    Level 6
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Thanks for your patience and for sharing the additional information! Let’s address the NullReferenceException you’re experiencing in the Player_Controller at line 244.

    From the error log you provided, it suggests that the issue occurs while trying to call the Move() function. This can happen if some components or variables within the Player_Controller are not properly initialized or assigned before the scene transition.

    Steps to Resolve the Issue:

    1. Check Player State Initialization: Since you mentioned the player character has a player state component, ensure that Player_State is properly initialized in the Awake method. Make sure you’re not trying to access it before it has been set.

      void Awake()
      {
          // Ensure this is set
          Player_State = GetComponent(); 
          if (Player_State == null)
          {
              Debug.LogError("Player_State is not attached to the Player_Controller.");
          }
      }
    2. Avoid Calling Move() in Cutscene State: In the Move() method, you should already be checking if the player is in a cutscene. However, it’s good to ensure that no other parts of the code are calling Move() or accessing player variables when transitioning scenes:

      private void Move()
      {
          if (Player_State.Healing || Player_State.Cutscene)
          {
              Rigidbody.velocity = new Vector2(0, 0);
              return;
          }
          // Existing movement code...
      }
    3. Player Object Persistence: Since the player object is not set to DontDestroyOnLoad, ensure that when you transition scenes, you instantiate your player correctly in every scene. You might be losing references if the player object is destroyed during the transition.

    4. Using Scene Manager to Maintain State: If you want to preserve the player state and avoid NullReferenceExceptions, consider keeping the player object persistent between scenes:

      void Awake()
      {
          if (Instance != null && Instance != this)
          {
              Destroy(gameObject); // Avoid duplicates
          }
          else
          {
              Instance = this;
              DontDestroyOnLoad(gameObject); // Keep this object alive during scene changes
          }
      }
    5. Debugging Null Checks: Add debugging logs before the line numbers where the exceptions occur to determine which variable is null. For example:

      Debug.Log("Checking Player_State: " + Player_State);

    Analyzing the Video and Console Output:

    From the video and the console output you provided, it seems that the player might be trying to access certain properties or methods while transitioning between scenes. Ensure that the transition logic properly waits for the scene to load before allowing player actions.

    If you still encounter the issue after these adjustments, please look at the specific lines in the Move() function and the context around line 244. If you can identify which variable is null, I can provide more focused help! Let me know how it goes!

    #19175
    Nirvik Rajbhandari
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    Sorry if I sound impatient, but have you found a way to fix it? If not, is there anything else I can provide to help?

    #19177
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::
    Sorry if I sound impatient, but have you found a way to fix it? If not, is there anything else I can provide to help?
    You’re not being impatient. Sorry for the delay. @josiah will help you with this in the next few hours.
    #19178
    Josiah Loh
    Level 3
    Moderator
    Helpful?
    Up
    0
    ::

    Hi @nirvikrajbhandari, Josiah here. I’ve sent another request to access your Google Drive project folders. Once I can access them, I’ll take a look and work on resolving the issues on my end. Thanks!

    #19181
    Nirvik Rajbhandari
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    Sorry, I didn’t see your request. I have shared access now. Thank you!

Viewing 11 posts - 21 through 31 (of 31 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: