Hi everyone, im not really familiar with the Unity Hub Questions and Answers Page (or ever you wanna call it) but im running into some sort of problem.
I am currently making a small project where the player is able to hide in a locker and under a table. I have it that when a player hides in a locker, the player object automatically disappears which is something, but whenever im interacting with the table that has a script attached to it. It doesnt seem to disappear. Rather it just stays stuck, this is really annoying because im trying to also get the player removed when hiding under a table. Could anyone help me out with this problem? Ive looked everywhere on how to solve this and i dont think the problem relies on the code itself. But i just had to ask it around.
This is the script for the table:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TriggerTable : MonoBehaviour
{
[SerializeField] private bool isHidingTable;
[SerializeField] private bool canHideTable;
[SerializeField] private Transform cameraTablePos;
[SerializeField] private new Transform camera;
[SerializeField] private GameObject player;
[SerializeField] private Camera playerCamera;
[SerializeField] private Camera tableCamera;
void Start()
{
cameraTablePos = GameObject.Find("TablePosition").transform;
player = GameObject.Find("Player");
playerCamera = GameObject.Find("Main Camera").GetComponent<Camera>();
tableCamera = GameObject.Find("TableCamera").GetComponent<Camera>();
playerCamera.enabled = true;
tableCamera.enabled = false;
tableCamera.transform.SetParent(null);
}
void switchToTableCam()
{
player.SetActive(false);
playerCamera.enabled = false;
tableCamera.enabled = true;
}
void switchToPlayerCam()
{
player.SetActive(true);
tableCamera.enabled = false;
playerCamera.enabled = true;
}
private void OnTriggerEnter(Collider collision)
{
if (collision.CompareTag("Player"))
{
canHideTable = true;
}
}
private void OnTriggerExit(Collider collision)
{
if (collision.CompareTag("Player"))
{
canHideTable = false;
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.E) && canHideTable)
{
isHidingTable = !isHidingTable;
}
if (isHidingTable)
{
tableCamera.transform.SetParent(null);
camera.parent = cameraTablePos;
camera.position = cameraTablePos.position;
switchToTableCam();
}
else
{
switchToPlayerCam();
}
}
}
This is the script for the locker:
using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
public class TriggerLockerDoor : MonoBehaviour
{
[SerializeField] private bool isHidingLocker;
[SerializeField] private bool canHideLocker;
[SerializeField] private Transform cameraLockerPos;
[SerializeField] private new Transform camera;
[SerializeField] private GameObject player;
[SerializeField] private Camera playerCamera;
[SerializeField] private Camera lockerCamera;
void Start()
{
cameraLockerPos = GameObject.Find("LockerPosition").transform;
player = GameObject.Find("Player");
playerCamera = GameObject.Find("Main Camera").GetComponent<Camera>();
lockerCamera = GameObject.Find("LockerCamera").GetComponent<Camera>();
playerCamera.enabled = true;
lockerCamera.enabled = false;
lockerCamera.transform.SetParent(null);
}
void switchToLockerCam()
{
playerCamera.enabled = false;
lockerCamera.enabled = true;
}
private void OnTriggerEnter(Collider collision)
{
if (collision.CompareTag("Player"))
{
canHideLocker = true;
}
}
private void OnTriggerExit(Collider collision)
{
if (collision.CompareTag("Player"))
{
canHideLocker = false;
}
}
void Update()
{
if (Input.GetKeyDown(KeyCode.E) && canHideLocker)
{
isHidingLocker = !isHidingLocker;
}
if (isHidingLocker)
{
lockerCamera.transform.SetParent(null);
camera.parent = cameraLockerPos;
player.SetActive(false);
camera.position = cameraLockerPos.position;
switchToLockerCam();
}
else
{
player.SetActive(true);
lockerCamera.enabled = false;
playerCamera.enabled = true;
}
}
}
See the hiearchy on the left side on the video to get a better overview with that i mean