Search Unity

Referring to Child Object Lights

Discussion in 'Scripting' started by Rehtael, Aug 19, 2017.

  1. Rehtael

    Rehtael

    Joined:
    Jun 20, 2017
    Posts:
    53
    So, this is probably a dumb question, but I haven't been able to find a straight answer. I'm trying to reference a child of a child which is a light that I'd like to increase the intensity of. I'd like the lights to light up when the mouse is over the parent.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class forgeClick : MonoBehaviour {
    6.  
    7.  
    8.     private void OnMouseDown()
    9.     {
    10.         Debug.Log("This is the Forge");
    11.     }
    12.  
    13.     private void OnMouseOver()
    14.     {
    15.         Debug.Log("Mousing over Forge");
    16.      
    17.     }
    The object of this script is "forge", the child of this object is "forgeLights", and the two children of that which I want to affect are "leftForgeLight" and "rightForgeLight".
     
  2. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Make the lights children on a parent "Lights" object, then:
    Code (CSharp):
    1. public GameObject Lights; // drag-drop this in Inspector
    2.  
    3. //in function
    4. Lights.SetActive(true); // false for off
     
  3. Rehtael

    Rehtael

    Joined:
    Jun 20, 2017
    Posts:
    53
    Thank you, though if I'm trying to change the intensity, how do I refer to that in script?

    Also, if I am trying to create several sets of lights which light up independently of one another, do I still child them under lights?
     
    Last edited: Aug 19, 2017
  4. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    To change intensity, read this: https://docs.unity3d.com/ScriptReference/Light-intensity.html

    You can really do what you want. If you have sets of lights, you could put them in groups as children of Lights. That way you could enable/disable Lights for all the lights, and then have different groups of lights you could adjust. But that's really up to you and should be decided by what you're trying to achieve in your project.
     
  5. Rehtael

    Rehtael

    Joined:
    Jun 20, 2017
    Posts:
    53
    Thanks, I appreciate the help. I wish more of the help pages and tutorials explained application over what the thing just does.