Basically what the title says. I want an array of light objects to turn off and back once they are interacted upon.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using SUPERCharacter;
public class PowerSwitch : MonoBehaviour, IInteractable
{
public bool Interact()
{
LightsOff();
return true;
}
void LightsOff()
{
GameObject[] AllLights = GameObject.FindGameObjectsWithTag ("Lights");
foreach(GameObject go in AllLights)
{
go.SetActive(!go.activeSelf) ;
}
}
}
The code you shared looks like it will do that. What’s the issue you are having with it?
I activate the button it turns off, but when i activate it again it does not do anything except for debug.log
*I want it to turn back on
That’s because “FindGameObjectsWithTag” does NOT find deactivated objects, as per the documentation:
Why not just find them all at the start and save the array?
Because I had no clue I could. How would I do that the way you mention it?
P.S Thank you for calling that out
The other thing is you could just disable the Light components rather than setting the whole GameObjects inactive.
is there a way i can call for all arrays in the “ ” bracket?
I don’t have the slightest idea what this question means.
system
December 12, 2023, 8:10am
9
I’m guessing you’re looking for something like this?
using UnityEngine;
using SUPERCharacter;
public class PowerSwitch : MonoBehaviour, IInteractable
{
private GameObject[] _allLights;
private void Start()
{
_allLights = GameObject.FindGameObjectsWithTag("Lights");
}
public bool Interact()
{
ToggleLights();
return true;
}
private void ToggleLights()
{
foreach (var go in _allLights)
{
if (go.TryGetComponent<Light>(out var component))
{
component.enabled = !component.enabled;
}
}
}
}
I’m guessing you’re looking for something like this?
using UnityEngine;
using SUPERCharacter;
public class PowerSwitch : MonoBehaviour, IInteractable
{
private GameObject[] _allLights;
private void Start()
{
_allLights = GameObject.FindGameObjectsWithTag("Lights");
}
public bool Interact()
{
ToggleLights();
return true;
}
private void ToggleLights()
{
foreach (var go in _allLights)
{
if (go.TryGetComponent<Light>(out var component))
{
component.enabled = !component.enabled;
}
}
}
}
Is there a way to make it timed.