Daniel Lip
Daniel Lip

Reputation: 11319

How can I switch the flag state for locking unlocking the doors?

This script is attached to the doors, There are many doors:

using UnityEngine;
using System.Collections;

public class HoriDoorManager : MonoBehaviour
{
    public DoorHori door1;
    public DoorHori door2;
    public bool doorLockState;

    void OnTriggerEnter()
    {
        if (doorLockState == false)
        {
            if (door1 != null)
            {
                door1.OpenDoor();
            }

            if (door2 != null)
            {
                door2.OpenDoor();
            }
        }
    }
}

And this script is attached to one empty GameObject:

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

public class DoorsLockManager : MonoBehaviour
{
    public bool locked;
    public bool lockStateRealTime = false;
    public Renderer rend;

    private Shader unlitcolor;
    private GameObject[] doorPlanes;

    private void Start()
    {
        doorPlanes = GameObject.FindGameObjectsWithTag("DoorPlane");
        ChangeColors(new Color32(255, 0, 0, 255), new Color32(0, 255, 0, 255));
    }

    private void ChangeMaterialSettings()
    {
        unlitcolor = Shader.Find("Unlit/Color");
        rend.material.shader = unlitcolor;
        rend.material.SetFloat("_Metallic", 1);
    }

    private void ChangeColors(Color32 lockedColor, Color32 unlockedColor)
    {
        for (int i = 0; i < doorPlanes.Length; i++)
        {
            rend = doorPlanes[i].GetComponent<Renderer>();
            ChangeMaterialSettings();
            if (locked)
            {
                rend.material.color = lockedColor;
            }
            else
            {
                rend.material.color = unlockedColor;
            }
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (lockStateRealTime)
        {
            ChangeColors(new Color32(255, 0, 0, 255), new Color32(0, 255, 0, 255));
            lockStateRealTime = false;
        }
    }
}

And last script that make the doors open and close:

using UnityEngine;
using System.Collections;

public class DoorHori : MonoBehaviour {

    public float translateValue;
    public float easeTime;
    public OTween.EaseType ease;
    public float waitTime;

    private Vector3 StartlocalPos;
    private Vector3 endlocalPos;

    private void Start(){
        StartlocalPos = transform.localPosition;    
        gameObject.isStatic = false;
    }

    public void OpenDoor(){
        OTween.ValueTo( gameObject,ease,0.0f,-translateValue,easeTime,0.0f,"StartOpen","UpdateOpenDoor","EndOpen");
        GetComponent<AudioSource>().Play();
    }

    private void UpdateOpenDoor(float f){       
        Vector3 pos = transform.TransformDirection( new Vector3( 1,0,0));
        transform.localPosition = StartlocalPos + pos*f;

    }

    private void UpdateCloseDoor(float f){      
        Vector3 pos = transform.TransformDirection( new Vector3( -f,0,0)) ;

        transform.localPosition = endlocalPos-pos;

    }

    private void EndOpen(){
        endlocalPos = transform.localPosition ;
        StartCoroutine( WaitToClose());
    }

    private IEnumerator WaitToClose(){

        yield return new WaitForSeconds(waitTime);
        OTween.ValueTo( gameObject,ease,0.0f,translateValue,easeTime,0.0f,"StartClose","UpdateCloseDoor","EndClose");
        GetComponent<AudioSource>().Play();
    }
}

When the locked flag is true in the DoorsLockManager script I want to make that the doors will not open in the HoriDoorManager and if the flag is false in DoorsLockManager then open the doors on HoriDoorManager.

Upvotes: 0

Views: 80

Answers (1)

Tricko
Tricko

Reputation: 551

One possible way is to make the DoorsLockManager's locked variable a static variable then Let HoriDoorManager use that for checking via if(DoorsLockManager.locked).

Another way is to convert DoorsLockManager to a Singleton so that the locked variable will remain a class variable.

Upvotes: 4

Related Questions