Forum rules - please read before posting.

The player is removed from ''DontDestroyOnLoad'

edited December 2024 in Technical Q&A

The Player through the adventure creator is always in the ''DontDestroyOnLoad' hierarchy between scene changes. A problem occurs when the player jumps to a platform and becomes a child of that platform through a custom script, unlinking itself from 'DontDestroyOnLoad''. The code is below, I tried it in several ways, even with AI, and I can't reverse it.

So because of this problem, every time the player changes scenes, the player is destroyed in the scene and restarted in the other.

public class PlataformaParent : MonoBehaviour
{

private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.gameObject.CompareTag("Player"))
    {
        // Defines the player as a child of the platform
        collision.transform.SetParent(transform);
    }
}

private void OnCollisionExit2D(Collision2D collision)
{
    if (collision.gameObject.tag == "Player")
    {         
        collision.transform.SetParent(null);
    }
}

}

Comments

  • Use a local Player for the scene, rather than relying on the prefab being spawned in.

    Drop your prefab into the scene while in Edit mode, and they'll replace the DontDestroyOnLoad one while that scene is open in Play mode.

  • It didn't work. I dropped the player prefab into the scene and the camera stopped working and the menus did not appear, some null errors appeared. I have a lot of custom scripts attached to the player. In AC game manager characters it is marked for two players, player 0 (Default) and player1. So I alternate between the two.

    Is there another way to force the player to remain in the hierarchy in "DontDestroyOnLoad' when the player leaves platforms?

    As I said, this problem arose because of mobile platforms in which the player jumps on top and in order not to bounce, the player ends up being a child object temporarily and this removes the player from the 'DontDestroyOnLoad' hierarchy, causing the player to restart when changing scenes. .

  • collision.transform.SetParent (null);
    DontDestroyOnLoad (collision.transform.gameObject);
    
  • collision.transform.SetParent (null);
    DontDestroyOnLoad (collision.transform.gameObject);

    It worked, thank you.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.