Perform an event on a component of all gameobjects in an array

Hi guys.

I’d like to

  • find all objects within a scene with a certain tag (“A” in this example)
  • then set all their meshrenderers to false

Through my searching so far I have come up with

void Update () {
		if (Input.GetKeyDown (KeyCode.K)) 
			GameObject[] objects = GameObject.FindGameObjectsWithTag("MusclesA");		

			for(int i=0; i<= objects.Length; i++)
				objects*.GetComponent<MeshRenderer>().renderer == false;*
  •  	}*
  •  }*
  • }*
    But I get this error:
    Assets/HideTaggedObjects.cs(21,90): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
    Any advice or hints appreciated!

You are using the equality operator (==).
Use the assignment operator (=).

objects*.GetComponent<MeshRenderer>().renderer = false;*

However, this will give you another error as renderer is read only and not a boolean type. To disable the renderer:
objects*.GetComponent().renderer.enabled = false;*

This line:

objects*.GetComponent<MeshRenderer>().renderer == false;*

has one “=” too many, and also components of type “MeshRenderer” don’t have a variable “renderer”. To disable the component, just use .enabled = false.
The complete line should look like this:
objects*.GetComponent().enabled = false;*
The “==” is mostly used as part of a if(valueA == valueB) type of check/comparison.

You’re not disabling the MeshRenderer correctly. It’s suppose to look like this:

objects*.GetComponent<Renderer>().enabled = false;*

I achieved what I needed to with

GameObject[] objects = GameObject.FindGameObjectsWithTag("A");	
			foreach(GameObject go in objects)
				go.GetComponent<MeshRenderer>().enabled = false;

It would be interesting to know why my previous code didn’t work however.