if statement in void update being blocked by previous if statement

Hi guys,
I have 2 sets of objects

  • I’m trying to toggle their meshrenderer components on and off using two separate keyboard inputs
  • my code works, kind of. The first key input works well to toggle the first set of objects on and off
  • however the second key input only works once the previous boolean state of the other key is false, and I don’t understand why. I get that code works in a linear fashion and it feels like it can’t ‘exit’ my first commented area.
  • I’ve flailed around with using else if and else but to no avail

Any hints would be greatly appreciated.

using UnityEngine;
using System.Collections;

public class HideTaggedObjects : MonoBehaviour {

public bool femoralOn = true;
public bool sciaticOn = true;

// Update is called once per frame
void Update () {

		//Grab the gameobjects with the relevant tags
		GameObject[] femoralObjects= GameObject.FindGameObjectsWithTag("FemoralNN");	
		GameObject[] sciaticObjects= GameObject.FindGameObjectsWithTag("SciaticNN");
		

		// use a boolean to set the state of the FEMORAL tagged objects
		if (Input.GetKeyDown (KeyCode.F)) 
			{
			Debug.Log ("F key was pressed");			
			femoralOn = !femoralOn;
			}

		if (femoralOn == true)
	    {
			for(var f = 0; f < femoralObjects.Length; f++)
			{
				femoralObjects[f].GetComponent<MeshRenderer>().enabled = true;
			}
		}

		if (femoralOn == false) 
			{
			for (var f = 0; f < femoralObjects.Length; f++) {
				femoralObjects [f].GetComponent<MeshRenderer> ().enabled = false;
			}
		//my code doesn't seem to want to 'exit' from here until the above bool state is false

		// use a boolean to set the state of the SCIATIC tagged objects
		if (Input.GetKeyDown (KeyCode.O))
			{
			Debug.Log ("O key was pressed");
			sciaticOn = !sciaticOn;
			}

		if (sciaticOn == true)
			{
				for (var s  = 0; s < sciaticObjects.Length; s++)
				{
					sciaticObjects ~~.GetComponent<MeshRenderer>().enabled = true;~~

~~ }~~
~~ }~~
~~ if (sciaticOn == false)~~
~~ {~~
~~ foreach(GameObject go in sciaticObjects)~~
~~ {~~
~~ go.GetComponent().enabled = false;~~
~~ }~~
~~ }~~
~~ }~~
~~ }~~
}

You have some problems with your braces…

if (femoralOn == false) 
{
    for (var f = 0; f < femoralObjects.Length; f++) {  <---  this
    femoralObjects [f].GetComponent<MeshRenderer> ().enabled = false;
}  <---  this closes the for loop  statement

That means that the first brace right after your if isn’t closed as you expect it to be, so the next if falls inside of that one.

You really need to learn about the importance of {brackets} in programming.

The section of code that you note doesn’t execute unless the above bool state is false, is because it’s contained within if (femoralOn == false) {...}