0
\$\begingroup\$
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DoorsManager : MonoBehaviour
{
    public bool globalDoorsLockState = false;
    public bool[] doorsLockState;
    public GameObject[] doorsShieldFX;


    // Start is called before the first frame update
    void Start()
    {
        doorsShieldFX = GameObject.FindGameObjectsWithTag("ShieldFX");
        doorsLockState = new bool[doorsShieldFX.Length];


    }

    // Update is called once per frame
    void Update()
    {
        for (int i = 0; i < doorsLockState.Length; i++)
        {
            if (globalDoorsLockState == true)
            {
                doorsLockState[i] = true;
            }
            else
            {
                doorsLockState[i] = false;
            }
        }
    }
}

I want to be able to change all the doors flags to true/false or each door individually. Now in the Update it's working only for the global.

How can I add code or change it so it will also be able to change each door lock state individually?

\$\endgroup\$
  • 1
    \$\begingroup\$ What is the purpose of duplicating a known value all over an array for every frame? Why not simply change all references to doorsLockState[whateverIndex] to simply directly refer to globalDoorsLockState itself? \$\endgroup\$ – Flater Nov 6 at 22:54
2
\$\begingroup\$

First of all I recommend not checking the doorLockState and globalDoorLock state in the update method. That method gets called on every frame (which impacts performance) and is better reserved for functions that actually needs to be checked constantly. (Like player movement)

Now back on topic you want to be able to change the lock state of every door individually as well as being able to change the lock state of every door at once. I recommend instead of storing the lockStates of every door in the DoorsManager (public bool[] doorsLockState) that you make a Door object that stores his own lock state. For that Door Object you can also create two methods that can change the lockState to either locked or unlocked.

To unlock (or lock) all doors at once you can still keep the globalDoorsLockState variable and control it using two methods similar to the Door object one that unlocks all doors and one that locks all doors. You can access these doors by storing the doors (instead of its lock states) in the DoorsManager.

Here is a simple example of how you can do it:

public class DoorsManager : MonoBehaviour
{
    public bool GlobalDoorsLockState = false;
    // Assign these in the inspector
    public Door[] DoorList;

    public void UnlockDoors() 
    {
        GlobalDoorsLockState = false;
        UpdateDoorsLockState();
    }

    public void LockDoors() 
    {
        GlobalDoorsLockState = true;
        UpdateDoorsLockState();
    }

    public void UpdateDoorsLockState() 
    {
        foreach(door in DoorList) 
        {
            if (GlobalDoorsLockState) 
            {
                door.LockDoor();
            }
            else 
            {
                door.UnlockDoor();
            }
        }
    }
}

public class Door : MonoBehaviour
{
    private bool lockState = true;

    public void UnlockDoor() 
    {
        // Inside this method you can also do more stuff like play a sound effect
        lockState = false;
    }

    public void LockDoor() 
    {
        lockState = true;
    }
}

Keep in mind that the Door class is supposed to be in a separate file.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.