- Home /
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!
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;
}
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!
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.
You don't need Linq to do ForEach on a List. I updated my answer to show how it's done in UnityScript (.js).
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.
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.