Search Unity

Collider and lights

Discussion in 'Scripting' started by denali5x, Jul 15, 2022.

  1. denali5x

    denali5x

    Joined:
    May 13, 2017
    Posts:
    27
    I figured this would be easy, but apparently I am not understanding something. This is a VR app.

    I have an oil lamp (parent) with a sphere collider and its a trigger
    I have a point light under it (child) and its "not active" (unchecked the box next to name)

    Oil Lamp
    |__point light

    I also have a torch, with a collider on the end, that the player carries. The idea here is, when the torch enters that sphere collider, the light should become enabled. Basically, the torch lights the oil lamp.


    This is the script so far:

    Code (CSharp):
    1.  
    2. void OnTriggerEnter(Collider col)
    3.     {
    4.  
    5.        
    6.         if (col.tag == "torch")
    7.         {
    8.             Debug.Log("Do something else here");
    9.             enabled = true;
    10.         }
    11.     }
    12.  
    I get the debug message, so I know the collision is working, what I cant figure out is how to target that light, and set it active. I am trying to make this generic, so I can set up multiple "oil lamps" without having to create a new script with specific names for each instance.

    Thanks
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Oh yah, yah, that's the basic way of Unity.

    Code (csharp):
    1. public Light TheLight;
    Fill that out in the inspector and then say:

    Code (csharp):
    1. TheLight.enabled = true;
    So each oil lamp would have an instance of this same script, but on different colliders, with different Lights dragged into the above slot.

    General case of it all:

    Referencing variables, fields, methods (anything non-static) in other script instances:

    https://forum.unity.com/threads/hel...-vars-in-another-script.1076825/#post-6944639

    https://forum.unity.com/threads/accessing-a-gameobject-in-different-scene.1103239/

    REMEMBER: it isn't always the best idea for everything to access everything else all over the place. For instance, it is BAD for the player to reach into an enemy and reduce his health.

    Instead there should be a function you call on the enemy to reduce his health. All the same rules apply for the above steps: the function must be public AND you need a reference to the class instance.

    That way the enemy (and only the enemy) has code to reduce his health and simultaneously do anything else, such as kill him or make him reel from the impact, and all that code is centralized in one place.
     
  3. denali5x

    denali5x

    Joined:
    May 13, 2017
    Posts:
    27
    Thank for that, I've tried something similar a few hours back.. still a no-go. I get the debug msg, but the light does not turn on.

    The collider is on oil lamp, as is the script, the light, a child, and is no enabled. This is where I thought I needed to target it directly, because the script is on the parent.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class lightTorch : MonoBehaviour
    7.  
    8.  
    9.  
    10. {
    11.     public Light TheLight;
    12.  
    13.     void OnTriggerEnter(Collider col)
    14.     {
    15.         if (col.tag == "torch")
    16.         {
    17.             //If the GameObject has the same tag as specified, output this message in the console
    18.             Debug.Log("Do something else here");
    19.             TheLight.enabled = true;
    20.         }
    21.     }
    22. }
    23.  
    24.  
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,689
    Remember the difference between Components (such as Lights and scripts) that can be .enabled or not, and the GameObject, which can only be .SetActive(true/false)

    Don't mix those two unless you track carefully what you are doing.
     
  5. denali5x

    denali5x

    Joined:
    May 13, 2017
    Posts:
    27
    Well, if I set the light to have a sphere collider and the script, then nothing happens, no debug msg, etc.
    I'm close, but I'm at a loss as to why its not functioning. I'll have to google a bit more.
     
  6. denali5x

    denali5x

    Joined:
    May 13, 2017
    Posts:
    27
    Ahh ok I get it. Thanks. Works now.
     
    Kurt-Dekker likes this.