1 Slider Controlling Two Objects Opacity Separatley

I have a working slider that controls the opacity of one object just fine. Now I need to make the same slider control a second object’s opacity as well and independent of one another. I can’t figure out how to store the opacity value for the two objects and then recall them when one or the other is selected so that the slider returns to where it was for the selected object.

Any help would be greatly appreciated.

When one of the objects is selected, change the value of the scroll variable to the opacity of the selected object.

When the slider value changes, set the opacity of the selected item to the value of the scroll variable.

Here's a simple example that works for any number of items:

var scrollValue  :  float;

/* array of things (can be any size) */

var thingList    :  SomeThing [];

/* which thing is currently selected */

var chosenThing  :  SomeThing;

var guiRect      :  Rect  =  Rect(200, 200, 200, 400);

class SomeThing
{
    var alphaValue  :  float   = 0.0;
    var name        :  String  =  "";
}

function chooseThing ( thingOffset : int )
{
    if ( ! thingList || thingList.length == 0 ) {
        return;
    }

    for ( var i = 0; i < thingList.length; i ++ ) {

        if ( i == thingOffset ) {
            scrollValue = thingList*.alphaValue;*
 _chosenThing = thingList*;*_
 _*}*_
 _*}*_
_*}*_
_*function Awake ()*_
_*{*_
 _*if ( ! thingList || thingList.length == 0 ) {*_
 _*Debug.LogWarning("Please initialize your things...");*_
 _*return;*_
 _*}*_
 _*initializeThings();*_
 <em>_/* start with the first thing */_</em>
 _*chosenThing = thingList[0];*_
_*}*_
_*function OnGUI ()*_
_*{*_
 _*GUILayout.BeginArea(guiRect);*_
 <em>_/* draw a button for each thing */_</em>
 _*for ( var i = 0; i < thingList.length; i ++ ) {*_
 _*if ( GUILayout.Button("Select Thing " + i) ) {*_
 _*chooseThing(i);*_
 _*}*_
 _*}*_
 <em>_/* display the chosen thing's alpha value */_</em>
 _*GUILayout.Box(chosenThing.name + " alpha value: " + scrollValue);*_
 _*scrollValue = GUILayout.HorizontalSlider(scrollValue, 0.0, 1.0);*_
 _*chosenThing.alphaValue = scrollValue;*_
 _*GUILayout.EndArea();*_
_*}*_
_*function initializeThings ()*_
_*{*_
 _*if ( ! thingList ) {*_
 _*return;*_
 _*}*_
 _*var counter = 0;*_
 <em>_/* give the things names */_</em>
 _*for ( var thisThing : SomeThing in thingList ) {*_
 _*thisThing.name = "Thing number " + counter;*_
 _*counter ++;*_
 _*}*_
 <em>_/* start with the first thing chosen */_</em>
 _*chosenThing = thingList[0];*_
_*}*_
_*```*_

Supposing that the slider script is attached to a 3rd object, you can store references to the semitransparent objects and the current alpha values of them, and draw in the same place only the slider of the selected object:

EDITED: You can have how many objects you want using an array: set the size and assign the objects to the array objects in the Inspector. The current alpha value of the selected object is directly read and modified by the slider.

// set the objects array size and fill its elements in the Inspector
var objects: Transform[];      // array with the objects to be controlled
var select : int = 0;          // selected object index
var rect = Rect(10,10,300,30); // slider control rect

function OnGUI () {
    // get the transparency of the currently selected object
    var alpha = objects[select].renderer.material.color.a;
    // adjust it with the slider
    alpha = GUI.HorizontalSlider(rect, alpha, 0.0, 1.0);
    // set the currently selected object transparency
    objects[select].renderer.material.color.a = alpha;
}