- Home /
The question is answered, right answer was accepted
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.
Are you trying to reach array itself? It should work as follows:
GameObject[] objects
// Initialization and such
objects[0].renderer.enable = true/false;
mmh, it doesn't work. I've got this error: Array index is out of range. I want to select ALL objects in the GameObject[], not just one :/
Array index out of range will occur if you try and access an item that has not been initialized in an array.
Really? You didn't even initialize your array? How do you expect it to work then?
Answer by LukaKotar · Jun 20, 2014 at 12:01 AM
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[i].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[i].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.
Answer by Kiwasi · Jun 20, 2014 at 12:05 AM
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[i]..... //Do whatever
}
Answer by xandermacleod · Jun 19, 2014 at 11:57 PM
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[i].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).
Your example has a lot of errors you might want to check:
private can't be used inside a function
If you want to access the Length property of an array, don't use square brackets []
In C# (assu$$anonymous$$g this should be C#) it's "Length" not "length"
The for-loop-variable should be initialized in the initialization section, usually with 0
It's "enabled", not "enable".
If an answers contains an example, it should work or at least be correct. Also since Unity supports 3 different languages it's usually a good idea to state which language you're example uses.
Follow this Question
Related Questions
if 4 objects touched the block it will be visible or unvisible 1 Answer
Reinitialize prefab 0 Answers
Find one inactive player (gameobject) 2 Answers
How can I control GameObject visibility? 2 Answers