URGENT help with opacity slider needed

Hi everyone

I have this set up so when the user clicks with right mouse button a little menu pops up with an opacity slider and a few buttons to change the selected objects texture.

The texture works fine. It only changes on the object which has been right mouse clicked. However the opacity changes on ALL objects this script is attached to. How do I make the opacity affect only the selected object?

Please help.

Thanks

//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;
slider = selected.renderer.material.color.a;
}

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

function Update () {
selected.renderer.material.color.a = slider; 
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 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);
      
       //Second button
         if (GUI.Button(Rect(10,65,90,20), "SOMETHING")) {           
         } 
       //Third button 
         if (GUI.Button(Rect(10,90,90,20), "SOMETHING")) { 
         }
       //Fourth button 
         if (GUI.Button(Rect(10,115,90,20), "SOMETHING")) { 
         }

    GUI.DragWindow (); 
}

OK so I have moved the slider from Update Function to Window Function and now it’s changing the material opacity one at a time. However when I select the next object it sets the opacity to the previously selected objects. ??? :?

//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 = selected.renderer.material.color.a; 
} 

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

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 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;
              
       //Second button 
         if (GUI.Button(Rect(10,65,90,20), "SOMETHING")) {            
         } 
       //Third button 
         if (GUI.Button(Rect(10,90,90,20), "SOMETHING")) { 
         } 
       //Fourth button 
         if (GUI.Button(Rect(10,115,90,20), "SOMETHING")) { 
         } 

    GUI.DragWindow (); 
}

I can’t see where selected is changing?

you need the raycast to change selected and you need to add or move

slider = selected.renderer.material.color.a;

to occur when selected changes, so you get the current value for the alpha for that object.

It seems to know when the selected object is changing with regards to which texture array it uses on the selected.

Admittedly it works best when I right click on an object, then close the resulting window popup and then right click on another object etc…

I made this code from scraps of other pieces of code, so I don’t know where I went wrong. The functions by themselves all work fine. But when put together in one script, they cause problems.

Cheers