player can only hide in one locker

when creating multiple prefab lockers, I can only hide in the one that was created first, and when im hiding in another ones, it gives this strange glitch

Here’s the video:

Here’s the code, it’s applied to the triggerbox inside locker model:

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

public class LockerHide : MonoBehaviour
{
    public Transform cameraHidePos;
    public Transform cameraOrigPos;
    public Transform camera;
    public GameObject player;
    public AudioSource heartbeatSound;
    public bool isHiding = false;
    private bool canHide = false;
   
    void Start()
    {
        cameraOrigPos = GameObject.Find("OrigPos").transform;
        camera = GameObject.Find("FirstPersonCharacter").transform;
        player = GameObject.Find("Player");
    }
   
    void OnTriggerEnter(Collider col)
    {
        if(col.transform.tag == "Player")
        {
            canHide = true;
            Debug.Log("can hide");
        }
    }
   
    void OnTriggerExit(Collider col)
    {
        if(col.transform.tag == "Player")
        {
            canHide = false;
            Debug.Log("cannot hide");
        }
    }

    void Update()
    {
        if(Input.GetKeyDown(KeyCode.E) && canHide)
        {
            isHiding = !isHiding;
        }
       
        if(isHiding)
        {
            camera.parent = cameraHidePos;
            player.SetActive(false);
            camera.position = cameraHidePos.position;
            camera.rotation = Quaternion.Euler(180, 0, 180);
            heartbeatSound.GetComponent<AudioSource>().enabled = true;
        }else
        {
            camera.parent = player.transform;
            player.SetActive(true);
            camera.position = cameraOrigPos.position;
            heartbeatSound.GetComponent<AudioSource>().enabled = false;
        }
    }
}

The problem is that for all four lockers, the “else” code is running anytime the player isn’t in that particular locker. When you’re in locker A, then lockers B and C are still telling the camera.parent to be the player transform, the player to be active, etc, etc. The only reason that the first-placed locker is working is because its Update code is running last, so the values it’s setting are overriding what the other lockers are doing.

A well designed system wouldn’t have the lockers directly controlling the camera and player at all, because collisions like this are inevitable. Rather, on the player control script, you could add something like

public LockerHide isInThisLocker; // this variable can be changed from outside this class

//in Update
if (isInThisLocker != null) {
camera.parent = isInThisLocker.cameraHidePos;
// etc
}
else {
camera.parent = transform;
//etc
}

And your LockerHide script then tells the player “hey, you’re hiding in me now”:

if(Input.GetKeyDown(KeyCode.E) && canHide && player.GetComponent<PlayerScriptName>().isInThisLocker == null)
        {
            player.GetComponent<PlayerScriptName>().isInThisLocker = this;
        }
        if (Input.GetKeyDown(KeyCode.E) && player.GetComponent<PlayerScriptName>().isInThisLocker == this) {
            player.GetComponent<PlayerScriptName>().isInThisLocker = null;
        }

And it lets the player script decide what to do with that information.

You can get fancier with this sort of thing, but this gets you to a much better system without any major structural changes.

thanks for the reply! ill try your solution

thanks, it worked!