Rotating a selected object

Hi

I have two objects on screen.
In play mode I want to be able to select an object and rotate using “r”.
This rotates the selected object ok, however after selecting 2nd object it now rotates both as opposed to the one I selected.

Any help or code segements appreciated.

Thanx

The reason is that you need a static variable of which object is selected or just one script which takes the clicked object’s Transform as a variable. You can compare components, for instance objects Transform or use the instance ID to compare integers.

Having a rotation script on each object could be solved with this addition:

static var selectedTransform : Transform;

if (selectedTransform==transform)
    // Rotation code here

Having one rotation script anywhere in the scene can be solved like this:

var selectedTransform : Transform;

if (selectedTransform!=null)
    // Rotation code here, make sure to address selectedTransform in the rotation

How you assign selectedTransform is up to you, it can either be from an OnMouseDown() or a RaycastHit, I prefer raycasting to stay nondependent to platform.

Try this,

  function Update(){
    var selectObj:Transform;
    var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    var hit : RaycastHit;

     if (Physics.Raycast (ray, hit, 100)) {
    selectObt=hit.collider.transform;
            }
    if(Input.GetKey(KeyCode.R)){
    selectObj.Rotate(Vector3(0, Time.deltaTime,0);
    }
}