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.
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).
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?
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).
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.
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
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!!