Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by ahgr123 · Feb 17, 2016 at 02:24 PM · scripting problem

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.....
             }
         }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by ThinhHB · Feb 17, 2016 at 04:58 PM

This's my simple solution :
  • 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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ahgr123 · Feb 18, 2016 at 09:41 AM 0
Share

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;
 
 
 
             }
 
         }
 
 
     }
     
 }
avatar image ThinhHB ahgr123 · Feb 20, 2016 at 09:44 AM 0
Share

Hi, Let modify your script a little : In OnTriggerEnter(), OnTriggerExit(), use :

col.gameobject.GetComponent();

=> It'll get Light component on collide objects

avatar image ahgr123 ThinhHB · Feb 20, 2016 at 01:35 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

37 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Make a Jump Delay 1 Answer

Instantiate with .obj 0 Answers

Player health script doesnt work? 2 Answers

Does anyone know why this simple player camera lock script is not working? 0 Answers

How to call a function when something happens in the game once conditions are met? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges