Renderer.enable for GameObject[]?

Hello,
I want to to do a renderer.enable = true/false for multiples GameObjects… But I don’t know how to do it? GameObject doesn’t have GameObject functions, so how I can do what I want please?

Cordially.

The simplest way to do that would be by using a foreach loop.

C#:

//Assuming the GameObject array variable is called 'gameObjects'
foreach(GameObject go in gameObjects) {
    //This will loop through all of the array items
    //You can refer to all the items individually with the 'go' variable
    go.renderer.enabled = false;
}

JavaScript:

//Assuming the GameObject array variable is called 'gameObjects'
//JavaScript does not support 'foreach', but it allows the same syntax with a 'for' loop
for(var go : GameObject in gameObjects) {
    //This will loop through all of the array items
    //You can refer to all the items individually with the 'go' variable
    go.renderer.enabled = false;
}

Another way would be using a ‘for’ loop, which preforms a bit faster.

C#:

//Again, I am assuming that the variable is called 'gameObjects'
//First we declare a variable
//Then we check if it is less than the length of the array (the loop will stop when the condition is false)
//And then we increase it by one every time the loop runs
for(int i = 0; i < gameObjects.Length; i++) {
    gameObjects*.renderer.enabled = false;*

}
JavaScript:
//Again, I am assuming that the variable is called ‘gameObjects’
//First we declare a variable
//Then we check if it is less than the length of the array (the loop will stop when the condition is false)
//And then we increase it by one every time the loop runs
for(var i : int = 0; i < gameObjects.Length; i++) {
gameObjects*.renderer.enabled = false;*
}
Please note that I am not completely sure about the syntax for JavaScript, so if you are using JavaScript, please let me know if it throws any errors.

There are multiple ways to handle this.

My current preference is foreach as follows (c# code)

GameObject[] gameObjects;

foreach (GameObject currentGameObject in gameObjects){
    currentGameObject.renderer.... //Do whatever
}

If order matters or you need more precise control you can use an old fashioned for loop

for (int i = 0; i < gameObjects.Length; i++){
   gameObjects*..... //Do whatever*

}

you will want to do a loop.

something along the lines of:

public GameObject[] objects;

//inside relevant function

private int arraySize;
arraySize = objects[].length;

if(arraySize > 0)
{
	for(int i; i < arraySize; i++)
	{
		objects*.renderer.enable = true/false;*
  • }*
    }
    make sure not to delete the array or alter its size before the function is complete or you will again receive an out of range error. (More of an issue with lists than normal arrays).