0
\$\begingroup\$

I have in the Hierarchy a door with a script attached to it.

DoorV

The script:

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 bool doorLocked = 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;
            }
        }
    }

    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();
    }
}

I added the doorLocked bool variable. And i want that when it's true and the door is on locked state to change the color of the two Point light 1 from green to red.

In the Start i did:

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

When i'm running the game and doorLocked is false this is how it looks like: Both lights are green and i selected one of the Point light 1 and you can see in the inspector the Color is green:

Green

Now this is a screenshot when the game is running and when lockedDoor is true. Now it should be red but it's only red around the Point light 1 the color seems to be still green:

Red

Now i saw that in the Hierarchy one object above Point light 1 is called Light_Small_02 it's a child also of Light_Small_02:

I marked it here with a black circle.

And i think that i need also to change this Light_Small_02 color from green to red. Not sure since i can't find any green color with it in the inspector but when i disable the Light_Small_02 the green color is gone.

Circle

Anyway the main goal is to change the color of the two lights according to the door state: doorLocked true/false

This is a screenshot of Light_Small_02 inspector showing the mesh renderer part:

MeshR

\$\endgroup\$
  • 1
    \$\begingroup\$ This is likely the limit on pixel lights. \$\endgroup\$ – Draco18s Dec 20 '17 at 2:53
  • 1
    \$\begingroup\$ You're only changing the light's color and not the mesh that "Light_Small_2" uses. Of course only the actual lighting effect's color will change and nothing else. \$\endgroup\$ – John Hamilton Dec 20 '17 at 5:06
  • \$\begingroup\$ @JohnHamilton I tried it now but it didn't change the color. I did in the code: public GameObject[] renderers; then in the loop: renderers[i].GetComponent<Renderer>().material.color = Color.red; and in the editor in the inspector dragged both "Light_Small_2" i'm not getting any errors but it's not changing it to red still green. \$\endgroup\$ – jdoedoe doedoe Dec 20 '17 at 10:42
  • \$\begingroup\$ @JohnHamilton And before that i tried: public Renderer[] renderers; and in the loop renderers[i].material.color = Color.red; but still they are green. \$\endgroup\$ – jdoedoe doedoe Dec 20 '17 at 10:43
  • \$\begingroup\$ Changing the color of the material might not always work, it seems as though you're using an atlas there, they work differently compared to the usual textures. Could you add a screenshot showing the mesh renderer (open the hidden parts too) on the Light_Small_2 object? \$\endgroup\$ – John Hamilton Dec 20 '17 at 10:55
2
\$\begingroup\$

Two problems here:

  1. A "Light" in a game scene is only an illumination, not a bright object itself. You'll see it via its effects on surrounding geometry, but it doesn't have a "bulb" or hot spot that you can see directly. The light seems to come from an invisible source.

    So, when we want to represent real light sources in our games, there are two parts:

    • The light component, to illuminate the surroundings.
    • A rendered visual to represent the source of the light. This could be a particle system representing the flames from a torch, or a flare component that draws a glare spot when we look toward the light's origin, or in this case, a bright spot on a mesh.

    In your case, it looks like the two green squares are part of the same mesh & material that's rendering the rest of your hallway. This won't give you the flexibility to change the colour of just this pair of glowing panels, without affecting other parts of your scene.

    You might want to replace these glowing squares with your own objects - say for example cubes or quads using an unlit shader and tinted to the green or red colour you want. Then you'll be able to address these elements individually, without wrestling with the rest of your corridor's rendering.

    You may get good enough results by placing the new objects to cover the original glowing spots, or you might have to cut those spots out of the corridor texture / mesh to prevent interference.

  2. Your lights are currently configured as "Baked," which means you're asking Unity to pre-compute their effect on the scene at edit time, using a high-quality light bouncing model, and use that pre-computed result at runtime. That won't work if you're changing the colour, intensity, or position of the lights during gameplay, so these should be marked "Realtime" instead.

    You've currently disabled baking, which is masking the issue, hence the warning text under the "Baked" Mode setting. I recommend still marking these lights as Realtime, just in case you later decide to experiment with baked lighting on the rest of your scene, to ensure these lights don't cause unsightly bugs if you forget to change them at that time.

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