- Home /
Using Bounds.Intersects with collider (trigger) and light
Hi,
I have a collider (trigger) which is component of a player and point lights scattered around in scene.
i would want when light (if more than one it simply affets just one of them) is inside the trigger and player presses button, the light component of the light gameobject disables (not the whole object because as far as I know disabled gameobject cant be reactivaed unless its kept in array) and when pressed again any disabled light components to be disabled again.
I know i will have to use boolean but i am having trobles declaring the variable of the collider and light to use bounds so i could use bounds.intersects. ?
TL;DR: How do i declare variables (Collider and light) so i can use bounds.intersects
using UnityEngine;
using System.Collections;
public class LightTurnOff : MonoBehaviour {
private Collider TriggerForLights = Collider.Bounds;
private GameObject LightA = GameObject.Light.Renderer.Bounds;
void Update () {
if (Input.GetKeyDown ("some key")) {
if (TriggerForLights.Intersects (LightA)) {
//do this.....
}
}
Answer by ThinhHB · Feb 17, 2016 at 04:58 PM
In Player script, create a List _lighList.
OnTriggerEnter(), check if the colide object have a Light component, then add it to our _lightList.
OnTriggerExit(). check if the exit object have a Light component, then remove it from _lightList if it exist in out _lightList
In Update(), if user press your "some key", then make a simple loop through our _lightList, enable or disable them.
Thanks for answer, I tried with what you said just to test if it disables but it doesn't seem to affect lights... Should i use array ins$$anonymous$$d or any other groups ?
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class LightsOff : $$anonymous$$onoBehaviour {
List<GameObject> LightsTurnOff = new List<GameObject>();
void OnTriggerEnter (Collider col) {
if (gameObject.GetComponent<Light>() != null) {
LightsTurnOff.Add(new GameObject());
}
}
void OnTriggerExit (Collider col) {
if (gameObject.GetComponent<Light> () != null) {
LightsTurnOff.Remove(GameObject());
}
}
void Update() {
if (Input.Get$$anonymous$$eyDown ("k")) {
if(LightsTurnOff.Contains(GameObject)){
GameObject.GetComponent<Light>().enabled = false;
}
}
}
}
Hi, Let modify your script a little : In OnTriggerEnter(), OnTriggerExit(), use :
col.gameobject.GetComponent();
=> It'll get Light component on collide objects
It would't really change that much to me...i made the script differently the only thing i dont know is how to select first object from the list ? I used the same as in array but its not correct - it produces error.
using System.Collections;
using System.Collections.Generic;
public class LightsOff : $$anonymous$$onoBehaviour {
private List<GameObject> ListOfLights = new List<GameObject> ();
void OnTriggerEnter (Collider col){
if (col.gameObject.tag == "Player") {
if (gameObject.tag == "Lg") {
ListOfLights.Add(new GameObject());
}
}
}
void OnTriggerExit (Collider col){
if (col.gameObject.tag == "Player") {
if (gameObject.tag == "Lg") {
ListOfLights.Remove(new GameObject());
}
}
}
void Update() {
if (Input.Get$$anonymous$$eyDown("k")) {
ListOfLights[0].GetComponent<Light>().enabled = false;
}
}
}
Your answer