Hey, guys. I’m doing an update for a game I just released yesterday. So, in my game, you must “activate” power generators by clicking on them. When one is activated, there is a Point Light that switches from red to green. However, right now, I can only get 1 generator light (the first one) to switch from red to green. And there are 10 of them. Does anybody know how I can make it so that ALL of the generator lights work properly? If so, I’d greatly appreciate some help. Thanks in advance!
Here’s the code:
public class ActivateGenerators : MonoBehaviour
{
public GameObject Player;
public GameObject Generator;
public GameObject redLight;
public GameObject greenLight;
public GameObject[] GeneratorArray;
void Start()
{
redLight.SetActive(true);
greenLight.SetActive(false);
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
if (hit.collider.CompareTag("Generator"))
redLight.SetActive(false);
greenLight.SetActive(true);
}
{
}
}
}
And here are the generators:
Any help would be greatly appreciated!
I assume you want each Generator to to turn its own light on. So you have to tag 10 generators to turn all the lights green? (not just 1 generator turns them all green).
I assume GeneratorArray is an array of all 10 generators gameObjects, and currently redLight and greenLight are just the first red/green lights dragged into this script from the inspector.
Your code also looks like it turns the red light off when you click on a Generator… but it will turn the green light on no matter what as long as you just click (which I assume is not what you intended).
You will need to make an array of all the red/green lights as well. You will also need a way to figure out which generator you clicked. You can either change the tags to Gen1, Gen2, etc. Or you can just loop through the generatorArray and figure out which one you clicked.
You will need to drag the red/green lights into the inpector array in the same order as the Generators:
using UnityEngine;
using System.Collections;
public class ActivateGenerators : MonoBehaviour
{
public GameObject Player;
public GameObject[] redLight;
public GameObject[] greenLight;
public GameObject[] GeneratorArray;
void Start()
{
foreach (GameObject light in redLight)
light.SetActive(true);
foreach (GameObject light in greenLight)
light.SetActive(false);
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.CompareTag("Generator"))
{
bool found = false;
int lightIndex = 0;
for (int index = 0; index < GeneratorArray.Length && !found; index++)
{
if (hit.collider.gameObject == GeneratorArray[index])
{
found = true;
lightIndex = index;
}
}
// if we hit a Generator and didn't find one in the array we got a problem
if (!found)
{
Debug.Log("Coudn't find " + hit.collider.gameObject.name + " in the Generator array!!");
}
else
{
greenLight[lightIndex].SetActive(true);
redLight[lightIndex].SetActive(false);
}
}
}
}
}
}
1 Like