How to save player position

I’m using a platform script but my problem is that upon exiting the Platform Trigger the player’s Y position changes a lot. I’m trying to fix this by saving the player’s position right before they leave the trigger and then reapplying it afterwards.


Here’s my code:



using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlatformAttach : MonoBehaviour
{
    public GameObject Player;
    double PositionPlayer;
    private void OnTriggerEnter(Collider other)
       
    {
        if (other.gameObject == Player)
        {
            Player.transform.parent = transform;
        }
    }

    private void OnTriggerExit(Collider other)
    {
        PlayerPosition = Player.transform.position.y;
        if (other.gameObject == Player)
        {
            Player.transform.parent = null;
            transform.position = new Vector3(transform.position.x, PlayerPosition, transform.position.z);
        }
    }
}

Try this code:

public GameObject Player;
// put those floats codes in the code when you want to save them like if you press a button
// this is the code of saving the values
float posX = Player.transform.position.x;
float posY = Player.transform.position.y;
float posZ= Player.transform.position.z;