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 20 posts - 21 through 40 (of 59 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 4
    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!

    #19186
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::
    Sorry, I didn’t see your request. I have shared access now. Thank you!
    I have sent a request to you as well. I will have a look at the files for you. Apologies for the delay.
    #19187
    Josiah Loh
    Level 4
    Moderator
    Helpful?
    Up
    0
    ::

    Hi @nirvikrajbhandari, I wasn’t able to find any new solutions beyond what Terence and Ser Apustaja have already suggested. Since the project was shared through Google Drive, the Unity project became corrupted when opened, which has made it difficult to work with reliably. With the current state of the project that I have access to, it’s not possible for me to accurately figure out where exactly the issues lie. Sorry for the inconvenience, and thank you for your patience.

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

    Oh ok, don’t worry and thank you for trying to help. I also sent access to Terence as well. Thank you!

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

    You’re welcome! I’m glad to hear that you’re working with others to resolve the issue. If you or Terence have any further questions or need additional assistance, feel free to reach out. Good luck with your project, and I hope you get everything working smoothly!

    #19190
    Terence
    Level 31
    Keymaster
    Helpful?
    Up
    0
    ::
    Oh ok, don’t worry and thank you for trying to help. I also sent access to Terence as well. Thank you!
    @nirvikrajbhandari your project files don’t include all of your files, so we can’t test it out. I will be releasing a video covering how you can put your project on GitHub, to ease filesharing and ensure that all your files get transferred. Google Drive isn’t the best tool for this.
    #19191
    Nirvik Rajbhandari
    Level 5
    Participant
    Helpful?
    Up
    0
    ::

    Oh ok. Also, I just realized I uploaded my folder as a regular folder should I have uploaded it as a zip or is that ok?

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

    It’s generally better to upload your project as a zip file, as it ensures that the entire folder structure and all files are preserved in one package. This makes it easier for others to download and access your project without missing any files. However, if the regular folder is accessible and contains all necessary files, it should be fine for now.

    If you have any specific concerns about the contents of the folder or if you think some files might be missing, please let me know!

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

    Yes, zip is an option, but it will be better if you put your project into GitHub. Here’s the guide — the first video by itself covers how to put your project onto GitHub, and the second video shows you how to add other collaborators into your project.

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

    I don’t know if I did it correctly. But here is the link for the GitHub page. Thank you!

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

Go to Login Page →


Advertisement below: