x


Count Builtin Array elements with a certain property.

I have a builtin array with 50 elements:

 var Tokens : GameObject[50];

The transform.x of the Tokens changes when the game runs. I want to know each moment how many of the Tokens have a transform.x > 0.

I know that looping through the array looks like this:

 var Tokens : GameObject[50];
 for (var i = 0; i <50; i++) {
     //Do something
 } 

Can I use this loop somehow, in order to count the elements (Tokens) of the array that have a transform.x>0?

more ▼

asked Dec 09, 2011 at 08:54 PM

schwertfisch gravatar image

schwertfisch
430 143 115 131

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

var theCount : int = 0;

for ( var thisToken : GameObject in Tokens ) {

 if ( thisToken.transform.position.x &gt; 0 ) {
     theCount ++;
 }

}

Debug.Log("GameObjects with positive x: " + theCount);

/ another way to write it that does the same thing: /

var theCount : int = 0;

for ( var i = 0; i < Tokens.length; i ++ ) {

 var thisToke : GameObject = Tokens[i];

 if ( thisToke.position.x &gt; 0 ) {
     theCount ++;
 }

}

Debug.Log("GameObjects with positive x: " + theCount);

more ▼

answered Dec 09, 2011 at 09:58 PM

jahroy gravatar image

jahroy
3.7k 37 46 80

Thank you.

Dec 10, 2011 at 07:25 AM schwertfisch
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2247
x534
x116
x90
x18

asked: Dec 09, 2011 at 08:54 PM

Seen: 1233 times

Last Updated: Dec 10, 2011 at 07:25 AM