What I want to do is something more or less very simple. The main bool flag lockDoors control all the doors and this is working fine now. But now I want to add a flag to each individual door so I can change the door lock state for each specific door. And if I change the lockDoors state it will change all the doors except if I changed some individuals.
I tried to add a new class name DoorControl and made it also Serializable but not sure if this is the way and how to do it.
Example of how it should look like in the Inspector:
lockDoors checkbox here (This is the flag for all the doors) and then:
Door1
checkbox here
Door2
checkbox here
Door3
checkbox here
So I can keep controlling the whole doors or each door individual.
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class DoorsLockManager : MonoBehaviour
{
[System.Serializable]
public class DoorControl
{
public bool doorLock;
}
public bool lockDoors = false;
public List<HoriDoorManager> _doors = new List<HoriDoorManager>();
public DoorControl[] doorscontrol;
private bool[] doorsLocks;
private Renderer rend;
private Shader unlitcolor;
private List<GameObject> DoorShieldFXLocked = new List<GameObject>();
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>());
}
doorscontrol = new DoorControl[_doors.Count];
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);
}
}
And this 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;
}
}