Rotate object by swipe rotates all objects

I have 2 cubes with the following script applied. The problem is when i swipe one object to rotate it it rotates both.
Can someone tell me why?

var targetItem : GameObject;

/********Rotation Variables*********/
var rotationRate : float = 1.0;
private var wasRotating;
var hit: RaycastHit;
private var layerMask = (1 <<  8) | (1 << 2);
function Start()
{
     layerMask =~ layerMask;   
}
function FixedUpdate()
{   
     if (Input.touchCount > 0)
     {        //    If there are touches...
             var theTouch : Touch = Input.GetTouch(0);        //    Cache Touch (0)
            
             var ray = Camera.main.ScreenPointToRay(theTouch.position);  
                
              if(Physics.Raycast(ray,hit,50,layerMask))
              {   
                 if(Input.touchCount == 1)
                         {                           
                             if (theTouch.phase == TouchPhase.Began)
                              {
                                  wasRotating = false;   
                              }       
                             
                              if (theTouch.phase == TouchPhase.Moved)
                              {
                                  
                                  targetItem.transform.Rotate(0, theTouch.deltaPosition.x * rotationRate,0,Space.World);
                                  wasRotating = true;
                              }       
             
                             }
                 }
           }
}

That is happening because that class that both cubes have on them is just looking to see if the first touch on screen hit something, it doesn’t use the info you gather in your ‘hit’ variable to determine what object to call your logic on.

I’ve done a tutorial on this subject (video on YouTube and text on my site). It’s in C# but the logic is pretty much the same. The main thing you’re missing is a line like I have on line 31 of my site:

//if the thing that was hit implements ITouchable3D
touchedObject = rayHitInfo.transform.GetComponent(typeof(ITouchable3D)) as ITouchable3D;

But in your script since you have this script on the actual object you could do something like:

if(hit.transform == this.transform)
    //Call the rotate function

Thanks for this. I will try it.