Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Jun 20, 2014 at 10:17 AM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by VSuper · Jun 19, 2014 at 10:54 PM · gameobjectrenderergameobjectsrenderer.enabled

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tanshaydar · Jun 19, 2014 at 10:57 PM 0
Share

Are you trying to reach array itself? It should work as follows:

 GameObject[] objects
 // Initialization and such
 objects[0].renderer.enable = true/false;


avatar image VSuper · Jun 19, 2014 at 11:14 PM 0
Share

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 :/

avatar image Kiwasi · Jun 20, 2014 at 12:08 AM 0
Share

Array index out of range will occur if you try and access an item that has not been initialized in an array.

avatar image Tanshaydar · Jun 20, 2014 at 09:15 AM 0
Share

Really? You didn't even initialize your array? How do you expect it to work then?

3 Replies

  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image VSuper · Jun 20, 2014 at 10:15 AM 0
Share

Thanks a lot :D

avatar image
2

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
 }



Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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).

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Jun 20, 2014 at 12:37 AM 1
Share

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

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

How can I hide a GameObject without active=false? 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges