I need a script to switch between two light sources, both children of the player. I have a code that can tell if it is time to switch the light sources, and to reset the variable after switching the lights. I just don’t know how to activate/deactive the game objects of the lights. The light sources are a sun and a torch.
using UnityEngine;
using System.Collections;
public class LightSourceManager : MonoBehaviour {
public bool switchLights = false;
void Update () {
if (switchLights) {
lightSwap ();
switchLights = false;
}
}
private void lightSwap () {
//Put code here
}
}
Try
if (light1.enabled==false){
light1.enabled=true;
light2.enabled=false;
}
else{
light2.enabled=true;
light1.enabled=false;
}
Pontik
4
public Light light1, light2;
public void switchLights()
{
if(light1 && light2)
{
if (light1.gameObject.activeInHierarchy)
{
light1.gameObject.SetActive(false);
light2.gameObject.SetActive(true);
}
else
{
light2.gameObject.SetActive(false);
light1.gameObject.SetActive(true);
}
}
}