- Home /
How can I control each door individual lock/unlock state ?
The first script is attached to a empty GameObject and is working fine, I can control all the doors and change the doors lock/unlock states and changing also the colors of the doors according to the lock states:
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class DoorsLockManager : MonoBehaviour
{
public bool lockDoors = false;
private Renderer rend;
private Shader unlitcolor;
private List<GameObject> DoorShieldFXLocked = new List<GameObject>();
private List<HoriDoorManager> _doors = new List<HoriDoorManager>();
private void Start()
{
DoorShieldFXLocked = GameObject.FindGameObjectsWithTag("DoorShield").ToList();
unlitcolor = Shader.Find("Unlit/ShieldFX");
var doors = GameObject.FindGameObjectsWithTag("Door");
foreach (var door in doors)
{
_doors.Add(door.GetComponent<HoriDoorManager>());
}
ChangeDoorsLockStates();
}
private void Update()
{
ChangeDoorsLockStates();
}
private void ChangeDoorsLockStates()
{
for (int i = 0; i < DoorShieldFXLocked.Count; i++)
{
if (lockDoors == true)
{
ChangeColors(Color.red, Color.green, i);
}
else
{
ChangeColors(Color.red, Color.green, i);
}
}
for (int x = 0; x < _doors.Count; x++)
{
if (lockDoors == true)
{
LockDoor(x);
}
else
{
UnlockDoor(x);
}
}
}
private void ChangeColors(Color32 lockedColor, Color32 unlockedColor, int index)
{
var renderer = DoorShieldFXLocked[index].GetComponent<Renderer>();
renderer.material.shader = Shader.Find("Unlit/ShieldFX");
if (lockDoors == true)
{
renderer.material.SetColor("_MainColor", lockedColor);
}
else
{
renderer.material.SetColor("_MainColor", unlockedColor);
}
}
public void LockDoor(int doorIndex)
{
_doors[doorIndex].ChangeLockState(true);
}
public void UnlockDoor(int doorIndex)
{
_doors[doorIndex].ChangeLockState(false);
}
}
The second script is attached to each door:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class HoriDoorManager : MonoBehaviour
{
private List<DoorHori> doors = new List<DoorHori>();
private bool doorLockState;
private void Start()
{
if (transform.parent != null)
{
Transform parent = transform.parent;
var children = parent.GetComponentsInChildren<Transform>();
if(children != null)
{
foreach (Transform door in children)
{
if (door.name == "Door_Left" || door.name == "Door_Right")
doors.Add(door.GetComponent<DoorHori>());
}
}
}
}
void OnTriggerEnter()
{
if (doorLockState == false)
{
if (doors != null)
{
for(int i =0; i < doors.Count; i++)
{
doors[i].OpenDoor();
}
}
}
}
public void ChangeLockState(bool lockState)
{
doorLockState = lockState;
}
}
What I want to do is some how to add to the second script the HoriDoorManager script a public bool flag and then in the first script the DoorsLockManager script to create a _doors List but that will contain also the bool flag of each door.
When going back to the editor and running the game I want to see in the inspector of the first script this structure example:
List of the _doors like this:
Door 1 checkbox here Door 2 checkbox here Door 3 checkbox here
So I can control and lock/unlock each door individual. Same like I control all the doors now with the colors change to do it for each door individual.
I'm adding a screenshot image show one of the doors example structure: The first script is attached to empty GameObject the second script is attached to each door Horizontal_Doors_Kit child. And the colors change are of the DoorShieldFXLocked (1) and DoorShieldFXLocked of each door.
The main goal is to be able to control the lock/unlock of each door individual.
Answer by WarmedxMints · Mar 12, 2019 at 04:53 PM
Remove the ChangeLockState Method in the HoriDoorManager script and replace it with this;
public bool GetLockState
{
get { return doorLockState; }
set { doorLockState = value; }
}
Change the List of doors to public in the DoorsLockManager script like this;
public List<HoriDoorManager> Doors = new List<HoriDoorManager>();
Then create a folder called Editor in your Assets folder and then add this DoorsLockManagerEditor.cs script in that folder
using UnityEditor;
[CustomEditor(typeof(DoorsLockManager))]
public class DoorsLockManagerEditor : Editor
{
DoorsLockManager _manager;
public override void OnInspectorGUI()
{
_manager = (DoorsLockManager)target;
base.OnInspectorGUI();
if (_manager.Doors.Count <= 0)
return;
for(var i = 0; i < _manager.Doors.Count; i++)
{
if (_manager.Doors[i] == null)
continue;
_manager.Doors[i].GetLockState = EditorGUILayout.Toggle("Door " + i + " Lockstate", _manager.Doors[i].GetLockState);
}
}
}
You will then be able to see a checkbox for each door in your manager script.
Answer by ForbiddenSoul · Mar 12, 2019 at 05:24 PM
I am not sure if you just want to use one script or both.
You could make a Door class, then attach that script to everything that is a door. You can then set the individual properties of each door on their own. Lockstate, a lock state custom color, or anything else you might want to add. Once you made the class you could add a Dictionary to your manager class that has indexed all of the doors. You can the look up the door in your dictionary to get a hold of a specific or multiple doors.
If you structure the way you access the .transform of your doors, all you really need is a door class attached to the GameObject and won't need a dictionary. Something wants to change the lock state of a door, that thing should know what the door is, and you change it.
"ChangeDoorsLockStates()" every update seems unnecessary. Simply change the lock state of one door, when that one door changes, not every update.
Your answer
Follow this Question
Related Questions
How can i pick two randomly items from gameobject array ? 1 Answer
Script for a hue rainbow scroll on the material 1 Answer
How can i using a break point if a gameobject have a collider after added to it ? 1 Answer
How can i get all childs from List ? 3 Answers
How can i set the same script on two GameObjects so the script will work on both objects same time ? 1 Answer