Why the original position is not the same when exit a car ?

I have on a same GameObject a Car I want to enter/exit this two scripts. The first one is to enter a car:

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

public class GetInCar : MonoBehaviour
{
   public GameObject mainCam;
   public Camera cam;
   public Transform playerTransform;
   public Vector3 originalPosition;
   public Quaternion originalRotation;

   // Start is called before the first frame update
   void Start()
   {
       originalPosition = playerTransform.position;
       originalRotation = playerTransform.rotation;
   }

   private void OnTriggerEnter(Collider other)
   {

       mainCam.SetActive(false);
       cam.enabled = true;
   }

   // Update is called once per frame
   void Update()
   {
       
   }
}

The second exit a car :

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

public class ExitCar : MonoBehaviour
{
   public GameObject mainCam;
   public Camera camera;
   public Transform playerTransform;
   public GetInCar getInCar;

   // Start is called before the first frame update
   void Start()
   {
       
   }

   // Update is called once per frame
   void Update()
   {
       if(Input.GetKeyDown(KeyCode.E))
       {
           playerTransform.position = getInCar.originalPosition;
           playerTransform.rotation = getInCar.originalRotation;

           camera.enabled = false;
           mainCam.SetActive(true);
       }
   }
}

The problem is when I’m getting into a car while inside the car if I press on one of the keys for example D then it will change the player position, The player will keep be sitting inside the car but when I press on E to exit the car it will position me the player at the position I pressed on D left many times instead on the originalPosition when I entered the car.

It will work fine if I will not press on D at all while sitting in the car. but when pressing on D while sitting in the car then the original position will be the current position I pressed on D and not the originalPosition from the GetInCar script.

This is a screenshot where I’m standing in front the car just before getting inside :

This is when I’m sitting in the car :

Now I pressed on E key while sitting in the car and I’m in the right original position :

but if I’m sitting in the car and first pressing many times on D and then pressing on E then the position is not the original :

The main goal is to save/keep the original position/rotation of the player transform so when existing the car it will position the player outside near the car just like before entering the car.

Updating :

Last thing I tried was to use a local private flag bool name hasEntered in the GetInCar script because I found when sitting in the car and pressing on A since it’s changing the player position it also detect again the OnTriggerEnter and set the originalPosition and originalRotation again but it didn’t help :

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

public class GetInCar : MonoBehaviour
{
    public GameObject mainCam;
    public Camera cam;
    public Transform playerTransform;
    public Vector3 originalPosition;
    public Quaternion originalRotation;

    private bool hasEntered = false;

    // Start is called before the first frame update
    void Start()
    {
       
    }

    private void OnTriggerEnter(Collider other)
    {
        if (hasEntered == false)
        {
            originalPosition = playerTransform.position;
            originalRotation = playerTransform.rotation;
        }

        hasEntered = true;
        mainCam.SetActive(false);
        cam.enabled = true;
    }

    // Update is called once per frame
    void Update()
    {
       
    }
}

Transform is a reference type. No matter how many times you copy it, it is still pointing to the same transform as before. Every copy of that refers to a single transform.

To store a position/rotation like that, make a Vector3 and Quaternion variable pair (those are value types) and assign the transform.position and transform.rotation to and from those new variables. That is actually a “copy the value” operation, which is why it’s called a value type.