Rotating objects using a for loop

This is more a standard scripting question, but I’m posting it in iPhone Dev because my issue may be with iPhone variable declaration issues.

I have spawned several prefabs and want to rotate them all by 45 degrees. They all have the tag “Block”. Here’s what I’ve currently got:

function RotateBlock () {
	var blocks = GameObject.FindGameObjectsWithTag ("Block");
		for (var block in blocks) 
		{ 
   			transform.Rotate(0, 0, 45); 
		}
	
}

I’m calling this through the standard iPhone touch function when I touch an object I’m temporarily using as a test. Unfortunately, it only seems to rotate the object I’m touching instead of the objects tagged “Block”. Since I’m getting this code from other examples, I’m assuming at least one of my problems is how I’m setting up the variable.

Thoughts?

This actually works???

I thought Unity would have killed your compile at

Interesting…

Just for interest sake, see what happens if you say

var blocks : GameObject[] = GameObject.FindGameObjectsWithTag ("Block");

or if that doesn’t work if you say

var blocks : Array() = GameObject.FindGameObjectsWithTag ("Block"); 
for (var block in blocks) 
      { 
           var obj : GameObject  = block; 
            obj.transform.Rotate(0, 0, 45); 
      }

I still can’t believe that Unity doesn’t complain about you not typing the variable… Interesting…

I actually have a similar problem with my iPhone app. I instantiate 3 characters and put them in an array. Using the same code as you above, I can access each character in turn ( I know this because when I call Destroy with it it works fine ) but when I set it’s animation to start playing, only the first element in the array is affected…

	for (x=0; x< Avatars.length; x++)
	{
		var Av	: Transform = Avatars[x];
		var obj : Animation = Av.FindObjectOfType(Animation);
		
		if (touchPositionDelta.x < 0)
			 obj.animation.Play("MoveLeft",PlayMode.StopAll);
		else obj.animation.Play("MoveRight", PlayMode.StopAll);
	}

So I am interested in hearing any replies to your post, also. You are not alone…

It is typed. Don’t make me explain type inference for the 1,000,000th time. :wink:

That’s because you’re saying “transform.rotate”, which refers to the object the script is on.

      for (var block in blocks) 
      { 
            block.transform.Rotate(0, 0, 45); 
      }

–Eric

OMG I missed that!!!

I was so focussed on the (non-)typed variable (non-declaration) that I didn’t notice the transform… OMG!

So do you have an answer to my code snippet also?

This was funny :slight_smile: He he…

Of course, the variable is declared as a type…gotta love type inference. :wink:

You’re doing “Av.FindObjectOfType(Animation);” which is only going to find the first object of that type in the scene. Use something like the code I posted above, but with animation instead of transform.

–Eric

Wow… That was a surprise… I was under the impression that if you use this insode an object you constrain it to that object and either get the element you want or NULL… I didn’t realize this always searches globally…

Interesting…

I will see if I can do it another way then…

Here is the deal. I have an object called Player and this contains the GameObject[ ] variable. I then instantiate 3 prefabs and parent them to this object. So what I want to do is to play the animation for each object…

This seems to work fine…

		Av = Avatars[x].Find("swat");
		obj = Av.animation;

Not as dynamic as I would have wanted, but certainly the easiest… and at least it works… :slight_smile:

Thanks for the assist :slight_smile: