Swapping Between Targets

Hi all

I’ve tried to approach this in a number of different ways, but am now well and truly stuck…

I want to right click on an object to bring up a menu. Then right click on another object to bring up that object’s menu and destroy the previous menu. All objects have the same “menu” code attached to them. At the moment it works, only if I click the toggle button in the menu to destroy it and then right click on the object to create a new one. Please help me.

//the colider 
var target: Transform; 
//the object 
var selected: GameObject; 
var posx: int; 
var posy: int; 
var ObjectName: String; 
var visible; 

//opacity changer 
static var slider : float; 

//texture changer 
var no = 0; 
var textures : TextureArray = selected.GetComponent(TextureArray); 

function Start () {
slider = 1.0;
} 

function Update () { 
if (Input.GetKey(KeyCode.LeftAlt)) 
        { 
        } 
    else 
        { 
    if (Input.GetMouseButtonDown(1)) 
    { 
    posx = Input.mousePosition.x; 
    posy = Input.mousePosition.y;    
    var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition); 
    var hit: RaycastHit;        
      if (Physics.Raycast(ray, hit)) { 
         if (hit.transform == target) { 
            visible = !visible; 
            } 
         }  
      } 
   } 
} 

function OnGUI() 
{ 
  if( visible ) 
  { 
    mywindow = GUI.Window (0, (Rect (posx, Screen.height - posy, 110, 160)), WindowFunction, ObjectName); 
  } 
}


function WindowFunction (windowID : int) {    
       //On-Off button 
       if (GUI.Button(Rect(90,0,14,14),"", "toggle")) { 
          visible = false; 
          } 
        //Left texture button 
         if (GUI.Button(Rect(10,20,40,20), "<<")) {  
            no += 1; 
          if (no >=textures.textures.length) { no = 0; } 
          selected.renderer.material.mainTexture = textures.textures[no];          
         } 
        //Right texture button 
         if (GUI.Button(Rect(60,20,40,20), ">>")) {  
            no -= 1; 
          if (no <=-textures.textures.length) { no = 0; } 
          selected.renderer.material.mainTexture = textures.textures[no];          
         } 
          
         slider = GUI.HorizontalSlider ( Rect(10,45,90,20), slider, 0, 1.0); 
         selected.renderer.material.color.a = slider;
                       
       //Third button 
         if (GUI.Button(Rect(10,90,40,20), "OFF")) {
         selected.SetActiveRecursively(!selected.active);
         visible = false;
         }     
       //Fourth button 
         if (GUI.Button(Rect(60,90,40,20), "ON")) {
         selected.SetActiveRecursively(selected.active); 
         }
}

I’m not to sure but how about having another if statement that checks if the target your clicking on is the same as the one that is currently active. If it is then it can close the previous window before opening the new window.

Thanks ForceX

But because the code on all objects is the same, how would I do that? The object becomes “selected” as soon as I right click on it. If I right click on it again then the menu disappears, as intended. Hence I have:

 (hit.transform == target) { 
            visible = !visible;

Is this the bit that’s causing the problem?

Thanks