Light & collision script help

Hey, I’m looking for some help on a script I cant seem to get to work.

my project involves mixing lights. so the 3 colored lights start on, and upon collision, they turn off. when you exit the collision they turn back on.

I want to add an if statement that says if in the collision and you press fire one the light will turn off and stay off upon exit. I can’t seem to get anything to work, any advice or feedback would be appreciated!

Here’s the code for the first part where the lights turn off on collision and on, on exit:

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

public class TurnOn : MonoBehaviour
{
    private Light myLight;
    public AudioClip mySound;


    // Start is called before the first frame update
    void Start()
    {
        myLight = GetComponent<Light>();
    }
    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            myLight.enabled = !myLight.enabled;
            GetComponent<AudioSource>().PlayOneShot(mySound);

        }

    }

    void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            myLight.enabled = !myLight.enabled;
            GetComponent<AudioSource>().PlayOneShot(mySound);
        }

    }

}

here’s what I was trying

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

public class test : MonoBehaviour
{
    private Light myLight;

    // Start is called before the first frame update
    void Start()
    {
      
    }

    // Update is called once per frame
    void OnTriggerEnter(Collider other)
    {
      
        if (Input.GetButtonDown("Fire1"))
        {
            myLight.enabled = false;
        }
    }
}

thank you!

A bool here will save you!

Something like this:

bool isPlayerInside;

void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
        isPlayerInside = true;
        }
    }
    void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
        isPlayerInside = false;
        myLight.enabled = true;//player leaves the zone so turn light back on
        }
    }

then under Update() you check if player is inside and if he hits fire then do the action you require

   if (isPlayerInside && Input.GetButtonDown("Fire1")){
        myLight.enabled = false;
        }
1 Like

I really appreciate the reply! I am a total newbie at unity and coding so anything I’ve done with lights has been found online. So showing me bool use is definitely helpful. I made some tweaks to what you said so that when I enter the collision the light turns off and as soon as I leave it turns back on.

The part that confuses me is when I’m standing in the collision and if I click fire 1, I want it to then “lock” the light off so I can exit and then just see the 2 other lights remaining on. When I run the code and actually click fire 1 in the game nothing happens and if I exit the trigger the light remains on. I’m thinking that’s because the code is saying if I exit turn on, but how can I have the mouse click override that, so when I leave it stays off?

EDIT: I want to mention when I leave it the way you suggested it works perfectly, however, I likely explained how I wanted the interaction to work wrong.

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

public class TurnOnUnity : MonoBehaviour
{
    private Light myLight;
    public AudioClip mySound;
    bool isPlayerInside;

    // Start is called before the first frame update
    void Start()
    {
        myLight = GetComponent<Light>();
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            isPlayerInside = true;
            myLight.enabled = false;
        }
    }

    void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            isPlayerInside = false;
            myLight.enabled = true;//player leaves the zone so turn light back on
        }
    }

    // Update is called once per frame
    void Update()
    {
        if (isPlayerInside && Input.GetButtonDown("Fire1"))
        {
            myLight.enabled = false;
        }
    }
}

Great!
Okay first off these are called triggers, player enter the trigger and so we do the stuff we need and if we leave the trigger we can call a function too. Collisions are similar but triggers are right for your thing.

So in order to solve a problem we have different and multiple choices but we must decide which fits our game/app/purpose itself… i don’t want to confuse you so probably just stick with the option number 1. I will give you some options:

  1. Add another bool, that only becomes true when player has click fire. And when you leave the area check if that bool is false to turn it on again otherwise remain off.

  2. Destroy the light when you fire, so you first want to check if the light exists before enabling/disabling it.

  3. Make a list of objects (lights), so when you leave the area you turn on all the lights in the list. If you don’t need a light to be turned on again then simply remove it from the list whenever it’s needed. (This probably would help you too since you are having multiple lights, check on YouTube videos about List)

1 Like

Ahh! I think I figured it out, it is working. Here’s what I ended up doing code wise:

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

public class TurnOnUnity : MonoBehaviour
{
    private Light myLight;
    public AudioClip mySound;
    bool isPlayerInside;
    bool PlayerFiresL;
    bool PlayerFiresU;

    // Start is called before the first frame update
    void Start()
    {
        myLight = GetComponent<Light>();
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            isPlayerInside = true;
            myLight.enabled = false;
        }
    }

    void OnTriggerExit(Collider other)
    {
        if (other.gameObject.tag == "Player")
        {
            isPlayerInside = false;
            myLight.enabled = true;//player leaves the zone so turn light back on
        }

        if (PlayerFiresL)
        {
            myLight.enabled = false;
        }

        if (PlayerFiresU)
        {
            myLight.enabled = true;
        }

    }

    // Update is called once per frame
    void Update()
    {
        if (isPlayerInside && Input.GetKeyUp(KeyCode.L))
        {
            PlayerFiresL = true;
            myLight.enabled = false;
        }

        if (isPlayerInside && Input.GetKeyUp(KeyCode.U))
        {
            PlayerFiresU = true;
            myLight.enabled = false;
        }
    }
}

Thank you VERY very much!

Not to add another question, but are you familiar with how one would change the color of lighting through a button click? I can’t find any syntax that would allow me to take a full intensity green (0,255,0) to say (0,128,0). If not no biggie this was a HUGE help.

Sure, but you should start a new topic on this one.

since it’s simple:

lt = GetComponent<Light>();
lt.color = Color.red;
//or
lt.color =  new Color(0,255,0);

more details:
https://docs.unity3d.com/ScriptReference/Light-color.html
https://docs.unity3d.com/ScriptReference/Color.html

1 Like

Also you can make it way more simple like this:

bool PlayerFired;

if (other.gameObject.tag == "Player")
{
 isPlayerInside = false;
if (PlayerFired == false)
{
 myLight.enabled = true
}
else
{         
 myLight.enabled = false
}
}
1 Like