Search Unity

How to make a script affect multiple objects

Discussion in 'Scripting' started by Digital-Phantom, Dec 17, 2014.

  1. Digital-Phantom

    Digital-Phantom

    Joined:
    May 31, 2013
    Posts:
    42
    I want to turn on/off multiple light sources (torch with flame effect) when the player enters certain areas within the game. I'm using the following script which works fine on a single item but I can't figure out how to make it affect all the objects/flames at the same time?

    Code (JavaScript):
    1. #pragma strict
    2.  
    3. var torch : GameObject;
    4.  
    5. function Start ()
    6. {
    7.     torch.SetActive(false);
    8. }
    9.  
    10. function OnTriggerEnter()
    11. {
    12.     torch.SetActive(true);
    13. }
    14.  
    15. function OnTriggerExit()
    16. {
    17.     torch.SetActive(false);
    18. }
    19.  
    (The items/objects are prefabs and untagged)

    Any ideas guys?
     
  2. TonyLi

    TonyLi

    Joined:
    Apr 10, 2012
    Posts:
    12,694
    Try this. (Pardon typos; I'm typing this directly into the post.)
    Code (csharp):
    1. #pragma strict
    2.  
    3. var torches : GameObject[];
    4.  
    5. function Start ()
    6. {
    7.   SetTorches(false);
    8. }
    9.  
    10. function OnTriggerEnter()
    11. {
    12.   SetTorches(true);
    13. }
    14.  
    15. function OnTriggerExit()
    16. {
    17.   SetTorches(false);
    18. }
    19.  
    20. function SetTorches(value : boolean)
    21. {
    22.   for (var torch in torches)
    23.   {
    24.     torch.SetActive(value);
    25.   }
    26. }
     
    image28 likes this.