horizontal GUI slider to control game object transparency

If I wanted to have a GUI slider to control the visibility of an object (from transparent to fully opaque), would I use a transparent shader or renderer.enabled?

I haven’t started researching this yet in the documentation, but was just considering where to begin.

Thanks so much.

You’ll want to use a transparent shader and then adjust the color (notably the alpha) from there. Toggling renderer.enabled is like an on/off switch, that doesn’t seem to mesh with your desired use of a slider (if you wanted on/off then it might be better to use a toggle or something).

Thanks H.

so I did something like this:

var slider = 0;
var a : float;

function OnGUI () {
	
	slider = GUI.HorizontalSlider( Rect(20,135,175,30), slider, 0, 255);
	
	if (slider <= 255){
   	a = 0;
	}
	
	else if (slider > 0){
   	a = 1;
	}
}

The slider doesn’t change the alpha value at either end - do you know what’s wrong here?

Also,do you have any suggestions for how to make the alpha channel change incrementally with the slider instead of having values of 0 or 1?

As Higgy said, you need to make sure you have a transparent shader applied. Then use code like this:

function OnGUI () { 
	slider = GUI.HorizontalSlider( Rect(20,135,175,30), slider, 0.0, 1.0);
	renderer.material.color.a = slider;
}

Cheers.

It worked like a charm.

In attempt to have the fully visible version on the right side of the slider, I switched the 1.0 and 0 like so:

var slider : float;

function OnGUI () {
   slider = GUI.HorizontalSlider( Rect(20,135,175,30), slider, 1.0, 0);
   renderer.material.color.a = slider;
}

However, it just switched the actual slider-head’s start location to the 1.0 side (the right side).

Do you know how to switch the location of the values so that 1 is on the left and 0 is on the right, but the slider-head begins on the left?

Thanks Thylaxene for your help!

Set your initial slider value to be 1.0.

var slider : float = 1.0;

function OnGUI () {
   slider = GUI.HorizontalSlider( Rect(20,135,175,30), slider, 1.0, 0);
   renderer.material.color.a = slider;
}

I would solve it like that
For example:

Variables:

Color defaultColor, transparancyColor;
float trans;

on the top of your OnGUI()

trans= GUI.VerticalScrollbar(new Rect(100, 100, 20, 100), panelTransparancy, 0.1f, 1.0f, 0.0f);
transparancyColor.a = trans;
GUI.color = transparancyColor;

and at the end, u need to init the colors, because if u dont, u just got black as color…
so in the start method

transparancyColor = GUI.color;
defaultColor = GUI.color;

And if u want differents part with no transparancy ull wirte GUI.color = defaultColor;
so ull get the def. colors back.

All u want to change the transparany would be written after GUI.color = transparancyColor;

thanks guys.

Hb, I tried setting my initial float value to 1 and the sliderhead is still on the left at runtime.

DaFame, I think the approach you suggested is over my head…but I still have to try it - I’ll let you know,

thanks again,
M

But isn’t that what you asked for?

If you want it on the left, set the initial value to 1.0, if you want it on the right, set the initial value to 0.0. The gist of this is that you’re telling the slider to use the range of 1.0 to 0.0 (left to right), so just set your default as you want (1.0 for the left, 0.0 for the right, something in between for something in between). :slight_smile:

I tried to do the same thing with a vertical bar and a Texture2D.

If i try :

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

I get :

Assets/Script perso/Perspikub.js(29,40): BCE0019: 'renderer' is not a member of 'UnityEngine.Texture2D'.

2DTexture seems to be different from an object. Anybody knows how resolve this problem ? :wink:

Textures do not have a renderer property just as the error says, and as per the docs:

Texture2D

If this code is attached to the game object whose opacity you want to control then just access the renderer directly (drop my2DTexture. entirely). Remember, the opacity you’re controlling has no relation at all to the texture itself. A game object uses a renderer component, that in turn uses a material to determine how it should be rendered (including a color and a texture among other properties).

it’s been a while, but now I’m back to working on this…

Hb…per the old posts, I was having trouble with the slider coordinating with the slider head. I made it confusing in the last reply, but what I would like is for the object to be visible (slider value=1) at runtime (and the slider head can be wherever it wants - left or right), and then to be invisible (at 0) as the slider head moves towards the zero side.

But the game object has a visibility value of 0 at runtime with the following code:

var slider : float=1;
function OnGUI () {
   slider = GUI.HorizontalSlider( Rect(20,135,175,30), slider, 1, 0);
   renderer.material.color.a = slider;
}

How can I make the visibility be 1 at initial run time?
Gracias in advance.

The default value of a variable set in a script is somewhat misleading. It will use that value only if the variable isn’t subsequently set in the editor. Try setting the value of the slider variable to 1 in the inspector.

it worked, thanks!

I’m having some difficulty getting the script to talk to the shader.
Also I get "Material doesn’t have a color property ‘-Color’ error from the Console.
Can anyone advise?
Thanks

If you’ve typed this exactly as it appeared, then the problem is that the first character should be an underscore (ie, “_Color”, not “-Color”).

This is the script and the error and the slider shows up but it doesn’t affect the shader:

var slider : float = 0.5;

function OnGUI () {
slider = GUI.HorizontalSlider( Rect(20,135,175,30), slider, 0.0, 1.0);
renderer.material.color.a = slider;
}

Not all shaders have a “_Color” property. You can do a “HasProperty” check first.

got it I had two shaders with the same name

ive been baning my head on this for hours I can control the RGB but not the alfa
Ive tried switching the shaders and checking the alfas in the texture files
HEEEEEEEELLLLLLLPP!!

ribsSlidervalue = GUI.VerticalSlider ( Rect(710, 340 , 200, 60), ribsSlidervalue, 0.0, 1.0);
//renderer.material.color.a = RibsSlidervalue;
SliderVisiablity(ribsSlidervalue,ribsPart);
}

function SliderVisiablity(VisVal:float,bodyPart:GameObject)
{
// bodyPart.renderer.material.color.a = VisVal;
//UnityEngine.Renderer:get_material(bodyPart);
//renderer.get_material(bodyPart);
//renderer.material.color.a = VisVal;
bodyPart.renderer.sharedMaterial.color.a =VisVal;
//bodyPart.renderer.sharedMaterial.color.r =VisVal ;
// bodyPart.renderer.sharedMaterial.color.g =VisVal;
// bodyPart.renderer.sharedMaterial.color.b =VisVal;
// RibsM.renderer.material.color.a = 0.5;
print(bodyPart.renderer.sharedMaterial.color.a);
print(VisVal);
// print(bodyPart);
}

Control Object Transparency with UI Slider