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 /
avatar image
0
Question by benmbaris · Dec 09, 2013 at 12:32 AM · javascriptlistarraysrenderer

Any way to enabled all GameObjects in an Array List? (UnityScript)

Hey all,

I am making a game that includes disabling and enabling the renderer of multiple objects. In my case, when the player starts, the game loads a certain amount of boxes on the screen. All of the boxes are in an array list, using System.Collections.Generic;

My problem is, when I use a for loop to enable the renderer on all of the objects, it does it one-by-one. Is there a way to make it so all of the objects are enabled at the same time? I tried messing with the .All() and .TrueForAll() methods found here MSDN but failed miserably with tons of errors. I figured it would be kind of tricky since I am using UnityScript.

Thanks!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Statement · Dec 09, 2013 at 01:14 AM

Examples of using List..ForEach in UnityScript


 #pragma strict
 
 // Since we are using List.< T >, we need to import this:
 import System.Collections.Generic;
 
 // A list of game objects we want to do something with.
 var gos : List.< GameObject >;
 
 function Start () {
      // Example 1: (If you KNOW that all game objects have a renderer)
      gos.ForEach(function(g) { g.renderer.enabled = true; } );
 
      // Example 2: (If you DONT KNOW if all game objects have a renderer)
      gos.ForEach(function(g) { if (g.renderer) g.renderer.enabled = true; } );
 
      // Example 3: (Same as Example 2, but cleaner)
      gos.ForEach(EnableRenderer);
      
      // Example 4: (Same as Example 2 and 3, but without delegates.)
      for (var g in gos)
         if (g.renderer) 
             g.renderer.enabled = true;        
 }
 
 // Belongs to Example 3:
 function EnableRenderer(g : GameObject) {
     if (g.renderer) 
         g.renderer.enabled = true;
 }
Comment
Add comment · Show 5 · 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 benmbaris · Dec 09, 2013 at 03:37 PM 0
Share

Hey Statement,

I have tried the foreach loop the unityscript way. This is what I came up with:

 for (var go : GameObject in listOfGameObjects)
 {
      go.renderer.enabled = true;
 }

This one had no errors, but it was still pretty slow. For example, if there are 4 boxes that need to be displayed, it will enable the renderer for the first box then wait for a few quick milliseconds, then display the other gameObjects. I know this is very $$anonymous$$or but it can become annoying haha.

I tried using the listOfGameObjects.ForEach command you mentioned, but it still just gave me a bunch of errors. I'm wondering if it is a monodevelop thing or if it just does not work in unityscript.

Thanks again for all the help!

avatar image flaviusxvii · Dec 09, 2013 at 06:02 PM 0
Share

You'll have to import Linq for the ForEach thing to work. UnityScript is an abo$$anonymous$$ation though, so it could be something else.

avatar image Statement · Dec 09, 2013 at 06:38 PM 0
Share

You don't need Linq to do ForEach on a List. I updated my answer to show how it's done in UnityScript (.js).

avatar image Statement · Dec 09, 2013 at 06:46 PM 0
Share

It still enables them one by one regardless of your method. I doubt there is any SI$$anonymous$$D capabilities for enabling multiple renderers :-)

Apologies if I am too obvious here, but if you're new to program$$anonymous$$g it is good to understand that regardless of what choice you take, eventually (even if it's hidden from your sight), some piece of code will go through each and one of them and for each object call your delegate.

avatar image benmbaris · Dec 10, 2013 at 03:01 PM 0
Share

Haha no worries. I am not new to program$$anonymous$$g at all, but I am by no means an expert. Thanks for the answer. It works much better than what I had.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

A node in a childnode? 1 Answer

Unity Crash on Adding to List Array, bug? 0 Answers

List.Add component as Renderer, can't access? 1 Answer

What's the best way to make a long scrolling list of items? 0 Answers

Using AnimationCurve in List. or Builtin Arrays 2 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