Variable changing randomly

Hello, my script wont work. I’ve started working on a door script to make adding doors easier. But the script just wont work, and I cant figure out why.

//Door
public GameObject door;

//The position of the door when open
public GameObject openDoor;

//Distance that the door opens
public float openDistance;

//The speed the door opens
public float openSpeed;

//The speed the door closes
public float closeSpeed;

//Player tag
public string playerTag;

//The player found by tag
GameObject player;

//Where the door starts
Transform startPos;

void Start()
{
    //Finds the player with said tag
    player = GameObject.FindGameObjectWithTag(playerTag);

    //Sets the start position of the door
    startPos = door.transform;
}

// Update is called once per frame
void Update()
{
    //Calculate the distance
    float distance = Vector3.Distance(player.transform.position, transform.position);

    //Open door
    if (distance <= openDistance)
    {
        //Calculate the movement
        float xPos = Mathf.Lerp(door.transform.position.x, openDoor.transform.position.x, openSpeed);
        float yPos = Mathf.Lerp(door.transform.position.y, openDoor.transform.position.y, openSpeed);
        float zPos = Mathf.Lerp(door.transform.position.z, openDoor.transform.position.z, openSpeed);

        //Set the position of the door
        door.transform.position = new Vector3(xPos, yPos, zPos);

        //Calculate the rotation
        float xRot = Mathf.Lerp(door.transform.rotation.x, openDoor.transform.rotation.x, openSpeed);
        float yRot = Mathf.Lerp(door.transform.rotation.y, openDoor.transform.rotation.y, openSpeed);
        float zRot = Mathf.Lerp(door.transform.rotation.z, openDoor.transform.rotation.z, openSpeed);

        //Set the rotation of the door
        door.transform.rotation = new Quaternion(xRot, yRot, zRot, door.transform.rotation.w);
    }

    //Close door
    if (distance >= openDistance)
    {
        //Calculate the movement
        float xPos = Mathf.Lerp(door.transform.position.x, startPos.position.x, closeSpeed);
        float yPos = Mathf.Lerp(door.transform.position.y, startPos.position.y, closeSpeed);
        float zPos = Mathf.Lerp(door.transform.position.z, startPos.position.z, closeSpeed);

        //Set the position of the door
        door.transform.position = new Vector3(xPos, yPos, zPos);

        //Calculate the rotation
        float xRot = Mathf.Lerp(door.transform.rotation.x, startPos.rotation.x, closeSpeed);
        float yRot = Mathf.Lerp(door.transform.rotation.y, startPos.rotation.y, closeSpeed);
        float zRot = Mathf.Lerp(door.transform.rotation.z, startPos.rotation.z, closeSpeed);

        //Set the rotation of the door
        door.transform.rotation = new Quaternion(xRot, yRot, zRot, door.transform.rotation.w);
    }
}

Thank you in advance, I’m sure I just missed something stupid…

EDIT,

The startPos variable is changing and I cant figure out why. The door moves, But won’t move back.
I have an empty game object that just holds all the components; the door object the script and the open door game object. The script was working perfectly, I exited out of unity and came back later and it wouldn’t work.

Couple questions/recommendations


Do you know which variable is changing? Would help with debugging.


Is openDoor a separate game object? If it’s inside of the gameObject with this script you could maybe have problems because it’s rotating this gameObject to the rotation of openDoor. But if it’s a child, then rotatingdoor will rotate openDoor, and you may end in an ever rotating door.


I made a mistake a couple days ago where I had my lerp time greater than one. I think it should be between 0 and 1. If setting in the inspector you could add change the code to

 //The speed the door opens
[Range(0, 1)] // might need to be [Range(0f, 1f)]?
 public float openSpeed;
 //The speed the door closes
[Range(0, 1)]
 public float closeSpeed;