1
\$\begingroup\$

Under Lights I have a Dim Lights and attached to it a script. Under the Dim Lights I have Point light and a Quad. And another Point light and quad.

In the screenshot the two lights and quads in Red are the one I added. And I want to switch between red and green using this quads and lights.

Lights

This is the DimLights script:

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

public class DimLights : MonoBehaviour
{
    //Lights Change
    public Light[] lightsToDim = null;

    private GameObject[] myLights;

    private void Start()
    {
        myLights = GameObject.FindGameObjectsWithTag("Light");
        for (int i = 0; i < lightsToDim.Length; i++)
        {
            lightsToDim[i].intensity = 0;
        }
    }

    public void LightsState(bool state)
    {
        foreach (GameObject go in myLights)
        {
            go.GetComponent<Light>().enabled = state;
        }
    }

    public IEnumerator dimLightOverTime(float toIntensity, float duration)
    {
        float counter = 0;
        //Get the current intensity of the Light 
        float[] startIntensity = new float[lightsToDim.Length];
        for (int i = 0; i < lightsToDim.Length; i++)
        {
            startIntensity[i] = lightsToDim[i].intensity;
        }

        while (counter < duration)
        {
            counter += Time.deltaTime;

            for (int i = 0; i < lightsToDim.Length; i++)
            {
                lightsToDim[i].intensity = Mathf.Lerp(startIntensity[i], toIntensity, counter / duration);
            }
            yield return null;
        }
    }
}

On the door there is a script Door Vert and in the Inspector in the bottom of the script there are two parameters: Door Locked and Locked. And it's not Locked the lights and quads should be in green when running the game. When it's locked they should be in Red.

Door

using UnityEngine;
using System.Collections;

public class DoorVert : MonoBehaviour {

    public float translateValue;
    public float easeTime;
    public OTween.EaseType ease;
    public float waitTime;
    public Light[] lt;
    public UnityEngine.Renderer[] renderers;
    public bool doorLocked = false;
    public bool locked = false;

    private Vector3 StartlocalPos;
    private Vector3 endlocalPos;


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

        if (doorLocked == true)
        {
            for (int i = 0; i < lt.Length; i++)
            {
                lt[i].color = Color.red;
                renderers[i].material.color = Color.red;
            }
        }
    }

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

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

    }

    private void UpdateCloseDoor(float f){
        if (doorLocked == false)
        {
            Vector3 pos = transform.TransformDirection(new Vector3(0, -f, 0));
            transform.localPosition = endlocalPos - pos;
        }

    }

    private void EndOpen(){
        if (doorLocked == false)
        {
            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();
    }

    private void Update()
    {
        if (locked == true)
        {
            doorLocked = true;
            for (int i = 0; i < lt.Length; i++)
            {
                lt[i].color = Color.red;
                renderers[i].material.color = Color.red;
            }
        }
        else
        {
            doorLocked = false;
            for (int i = 0; i < lt.Length; i++)
            {
                lt[i].color = Color.green;
                renderers[i].material.color = Color.green;
            }
        }
    }
}

The problem is that no matter what the door state is if it's not Locked it's all the time red when running the game. Never green.

In the Dim Lights script in the inspector I added the two Point lights to the Lights To Dim and both Quad and Quad (1) I changed them to be tagged as Light.

Lights

Not sure what I messed but it's not changing it to green when the door state start as unclocked.

On the door script the Door Vert script there is array variable:

public Light[] lt;

I forgot to fill it so I changed the size in the inspector to 2 and added the two Point lights to it. But still it's not working.

Last is a script I forgot to mention before Objects Manipulation: The script is also using the Dim Lights script.

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

public class ObjectsManipulation : MonoBehaviour
{
    //Camera
    public Camera playerCamera;

    //Scaling
    private Scaling scaling;

    //Lights
    public DimLights dimlights;
    private Coroutine lightCoroutine;

    //Colors
    private Colors colors;

    //Rotating
    public bool stopRotation = false;
    private Rotating rotating;

    private void Start()
    {
        scaling = GetComponent<Scaling>();
        scaling.Inits();

        colors = GetComponent<Colors>();
        colors.Start();

        rotating = GetComponent<Rotating>();
    }

    // Use this for initialization
    void Update()
    {
        //Scaling
        if (Input.GetKeyDown(KeyCode.F))
        {
            //Flip the scale direction when F key is pressed
            scaling.scaleUp = !scaling.scaleUp;

            //Stop old coroutine
            if (scaling.scaleCoroutine != null)
                StopCoroutine(scaling.scaleCoroutine);

            if (lightCoroutine != null)
                StopCoroutine(lightCoroutine);


            //Scale  up
            if (scaling.scaleUp)
            {
                //Start new coroutine and scale up within 5 seconds and return the coroutine reference
                rotating.rotateBack = false;
                scaling.scaleCoroutine = StartCoroutine(scaling.scaleOverTime(scaling.objectToScale, scaling.maxSize, scaling.duration));
                lightCoroutine = StartCoroutine(dimlights.dimLightOverTime(1, scaling.duration));
            }

            //Scale Down
            else
            {
                //Start new coroutine and scale up within 5 seconds and return the coroutine reference
                rotating.rotateBack = true;
                scaling.scaleCoroutine = StartCoroutine(scaling.scaleOverTime(scaling.objectToScale, scaling.minSize, scaling.duration));
                lightCoroutine = StartCoroutine(dimlights.dimLightOverTime(0, scaling.duration)); ;
            }
        }

        //Rotate
        if (Input.GetKey(KeyCode.R) && !scaling.scaleUp)
        {
            rotating.x += Time.deltaTime * rotating.rotationSpeed;
            scaling.objectToScale.transform.localRotation = Quaternion.Euler(0, 0, rotating.x);
            rotating.keyPressed = true;
        }
        if (Input.GetKeyUp(KeyCode.R))
        {
            rotating.keyPressed = false;
        }

        if (!rotating.keyPressed && !scaling.scaleUp && rotating.rotateBack == false
            && DetectInteractable.detected == false)
        {
            scaling.objectToScale.transform.rotation = Quaternion.LookRotation(playerCamera.transform.forward);
        }

        if (DetectInteractable.detected == true && !scaling.scaleUp && stopRotation == false)
        {
            rotating.x += Time.deltaTime * rotating.rotationSpeed;
            scaling.objectToScale.transform.localRotation = Quaternion.Euler(0, 0, rotating.x);
        }
    }
}
\$\endgroup\$
  • \$\begingroup\$ I managed to solve it. Should I delete the question ? \$\endgroup\$ – jdoedoe doedoe Mar 3 '18 at 21:39
  • 1
    \$\begingroup\$ Don't delete it, post your solution as an answer \$\endgroup\$ – user3797758 Mar 3 '18 at 21:49
1
\$\begingroup\$

The solution is to change the arrays size in the Door Vert script in the Inspector of the Lt and Renderers to 2.

In the Lt to add the two Point lights and in the Renderers to add the two Quads.

Door Vert

Now when I'm running the game and the door is locked the two quads are in red. And when I press on F and it's scaling the object (NAVI in my case) then it's also turning the lights on. Same when the door is unlocked with the green color.

In this specific scene I wanted some dark room and when the object is scaling fade in the lights turn on when the object is scaling fade out the lights are off.

I and others can play with this scripts a lot.

You can make for example that the lights will be turn on all the time. And then when changing the door state lock/locked it will change the quads and lights colors. You can also make the lights and quads each two a prefab. And much more.

For my needs it's working great.

\$\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.