Toggle headlights on/off using spacebar.

Using C sharp.

I made a game object reference in my script. The game objects is the parent oject to four lights. I am trying to solve a script that will use the space bar to toggle the parent object active.

I currently have:

If (Input.GetKeyDown(“space”)
headLights.SetActive (true);

If (headLights == true & Input.GetKeyDown(“space”))
headLights.SetActive (false);

If i comment out the second if statement it will turn on but not off. I think its the second if statement.
Not sure. Any help would be appreciated.

You can do it like this:

bool headlightActive = false;

If (Input.GetKeyDown("space") {
    headlightActive = !headlightActive;
    headLights.SetActive (headlightActive);

And add the code with this script above to an another object from which you should get a reference to your Head Light object using GetComponent().

Is this script attached to the parent or any of the lights? Because that’s exactly the problem. Once one GameObject is set to inactive it won’t execute any of its code. You should place that code in another object (one that won’t be set inactive) and set a reference to your lights parent game object.