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.
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;
}