I am making a script in which you press a button and a camera toggles to active (so that when you click that button, it changes view). There are two buttons, both in which toggle a camera active. If I click the opposite button, then go back and click the other button, it will not do anything. I thought about it for a moment and realized that it was because the camera had already been toggled on and active, so it would do nothing. I need to make button number two both toggle this camera active but also toggle the opposite camera inactive. Here is part of my script:
function OnGUI () {
if (GUI.Button(Rect(10,50,100,30), "Toggle Camera 1"))
cameraOne.active = true;
if (GUI.Button(Rect(10,80,100,30), "Toggle Camera Two"))
cameraTwo.active = true;
}
Thanks in advance to anybody who helps!
Wasn’t sure how you were getting your cameraOne and cameraTwo GameObjects so I didn’t include that code, and this assumes you will already have them.
With that in mind, you could try something like this:
public class ToggleButton extends MonoBehaviour
{
private enum State {ShowButtonOne, ShowButtonTwo}
private var m_currentState:State;
private var cameraOne:GameObject;
private var cameraTwo:GameObject;
private function Start()
{
m_currentState = State.ShowButtonTwo;
cameraOne.enabled = true;
cameraOne.disabled = false;
}
private function OnGUI()
{
switch(m_currentState)
{
case State.ShowButtonOne:
if(GUI.Button(Rect(10,50,100,30), "Camera 1"))
{
cameraOne.enabled = true;
cameraOne.disabled = false;
m_currentState = State.ShowButtonTwo;
}
break;
case State.ShowButtonTwo:
if(GUI.Button(Rect(10,50,100,30), "Camera 2"))
{
cameraOne.enabled = false;
cameraOne.disabled = true;
m_currentState = State.ShowButtonOne;
}
break;
}
}
}
Hopefully it’s not that complex. Let me know if there are parts of it that you don’t understand.
function OnGUI () {
}
function Update () {
if(Input.GetKeyDown(“2”)){ // pr use a button
camera.rect = Rect (0,0,1,1);
}
if(Input.GetKeyDown(“1”)){ //or use a button
camera.rect = Rect (0,0,0,0);
}
}
attach it to your camera
duplicate the script and add them to other cameras and change the functions
when you are using buttons sumply say
var button1 : boolean = false;
button1 = false
and inside OnGUI do this
button1 = (GUI.button(rect(… =] have fun