- Home /
How to make gameObject and GUI button invisible/visible?
I'm trying to use GUI buttons as switches between two lights; a flashlight, and the sun. I also want the buttons that operate the flashlight rotation to disappear when the sun is activated. However, I can't get the flashlight and buttons to become invisible. Also the reset button, which is supposed to reset the flashlight's position as well as the sliders, isn't working properly; it resets the sliders without moving the flashlight.
var fLight: Light;
var sunLight: Light;
var flashlight: GameObject;
private var fon = false;
private var son = false;
var startPosition : Vector3;
var startRotation : Quaternion;
var vSliderValue: float = 0.0;
var hSliderValue: float = 0.0;
private var reset = false; // object resets if true;
function Start()
{
fLight.light.intensity = .51;
sunLight.light.intensity = 0;
startPosition = transform.position;
startRotation = transform.rotation;
}
function Update()
{
if (fon) sunLight.light.intensity = 0;
if (fon) fLight.light.intensity = .51;
if (fon) flashlight.gameObject.active = true;
if (son) fLight.light.intensity = 0;
if (son) sunLight.light.intensity = .5;
if (son) flashlight.gameObject.active = false;
if (son) reset.active = false;
if (son) hSliderValue.active = false;
if (son) vSliderValue.active = false;
if (vSliderValue) flashlight.transform.rotation.eulerAngles.x = vSliderValue;
if (hSliderValue) flashlight.transform.rotation.eulerAngles.z = hSliderValue;
if (reset) transform.position = startPosition;
if (reset) transform.rotation = startRotation;
if (reset) vSliderValue = 0;
if (reset) hSliderValue = 0;
}
function OnGUI()
{
vSliderValue = GUI.VerticalSlider(Rect(10,20,10,100), vSliderValue, -50, 50);
hSliderValue = GUI.HorizontalSlider(Rect(20,10,100,10), hSliderValue, -50, 50);
reset = GUI.Button(Rect(40,40,50,50), "Reset");
fon = GUI.Button(Rect(900,10,100,100), "Flashlight");
son = GUI.Button(Rect(900,120,100,100), "Sun Light");
}
Answer by whydoidoit · Jul 08, 2012 at 07:23 PM
Ok so there is quite a lot wrong with your style of coding here - you should not be doing anything in Update to start with and you should make all of your ifs multi line using { and }.
You can have all of your if logic in the OnGUI -
fon = GUI.Button(Rect(900,10,100,100). "Flashlight");
if(fon) {
sunLight.light.intensity = 0;
flight.light.intensity = 0.51;
//etc etc
}
reset.active makes no sense. Add a #pragma strict to the top of your code to reveal some of the errors. I can't see where you set reset to true in this code either.
Sorry it's sloppy! Not entirely sure what I'm doing if that wasn't already obvious. I've moved all of the logic to OnGUI like you said. Something still isn't right with the reset, as it will only move the sliders back. What should I use ins$$anonymous$$d of .active to hide buttons in the GUI and to hide the flashlight?
:) Sloppy is O$$anonymous$$ - but it is clearer when you keep that logic together. Don't put code in Update when you don't need it :)
So the reset button should do something like:
reset = GUI.Button(Rect(40,40,50,50), "Reset"));
if(reset) {
flashlight.gameObject.active = true;
//etc
}
You could use light.enabled rather than disabling the whole gameObject.
When I use flashlight.gameObject.active = false; it does indeed become 'inactive' but I also want it hidden. How can I do that? I haven't been able to get a .renderer to work at all.
This is what I have for reset, but the flashlight won't go back to startPosition/Rotation.
reset = GUI.Button(Rect(40,40,50,50), "Reset"); if (reset) { reset = true; transform.position = startPosition; transform.rotation = startRotation; vSliderValue = 0; hSliderValue = 0; }
Well you can do:
for(var r : Renderer in GetComponentsInChildren(Renderer))
{
r.enabled = false;
}
code in the update is better then the ongui
on gui runs twice every frame
update only once
so if you put the same code in the ongui, it's twice as heavy for your computer
Answer by zirkl003 · Jul 17, 2012 at 01:00 AM
I don't know why I was completely unable to hide the flashlight in this scenario; instead, I moved it out of the camera's view and back again (using transform) each time the 'reset' button is hit.