Noob Question: Open/Close active panel.

Solution:

3 scripts:
Panelhandler.cs, playerdata.cs attached to your UICamera, in my case it was called the default camera and ButtonHandler.cs which is attached to all your buttons.

Panelhandler has an array in it: public GameObject panelselected; Select your camera and drag Panelhandler.cs into it. Find Panelhandler script in inspector and increase to the size to the amount of panels you have. If you have 10 panels, the size should be 9. Drag the panels that you want to be active into each of these array elements. Your buttons will be calling these panels.

Player data just has a variable in it to close the last active panel.

Finally, ButtonHandler.cs has 2 main variables. Var1=1 Opens Panel. Var1=2 will close it. Var2 is the element of your array. Var2=0 will call the first panel you put into your array. So on your Button Var1=1, Var2=0 will open your first panel. Var1=1, Var2=1 will open your second panel and of course close all the other panels.

I’ve attached my three scripts below.

Playerdata:

using UnityEngine;
using System.Collections;

public class playerdata : MonoBehaviour {

	public static int lastactive = -1;
	public static int energy = 0;
	
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
	
	}
}

Panelhandler:

using UnityEngine;
using System.Collections;

public class panelhandler : MonoBehaviour {

public GameObject[] gamepanels;

void Start () {	
}

void Update () {
	}

}

Finally, ButtonManager

using UnityEngine;
using System.Collections;

public class ButtonManager : MonoBehaviour {

// Var1 = Example: Var1 = 1,Open, Var1 = 2,close.
public static panelhandler panelselection;
public int var1 = 0;
//Var2 = Example: Selects slectpanel[] from Panelhandler.cs
public int var2 = 0;
private int lastact = 0;
void Start ()
{
	panelselection = GameObject.Find("Camera").camera.GetComponent<panelhandler>();
}

public void OnClick()
{
	if(var1==1){
		lastact = playerdata.lastactive;
		if(lastact != -1){
			panelselection.panelselected[lastact].SetActive (false);
		}
		playerdata.lastactive = var2;
		panelselection.panelselected[var2].SetActive (true);
		//Debug.Log (lastactive);
		
	}
}

}

Hi Everyone.

I’m very new to coding and I’ve created (really just amalgamated bit from here and there) two scripts that allow me to press a button and have a panel appear and then closed when another button (say ‘x’) is pressed.

What I’m trying to do is: I have 3 buttons. Button0, Button1 and Button2 and three panels called Panel0, Panel1 and Panel2. Basically what I want is that when I click Button0, Panel0 is set to active. When I press Button1 I need Pannel1 to become active (I have this now). Right now if I press Button0 and then Button1 both Panel0 and Panel1 are active, I only want one of the buttons/panels to be active at a time. Any suggestions? Bellow is my code.

public static PanelSelector panelselected;
public int var1 = 0;
public int var2 = 0;
private int lastactive = 0;
	
void Start () {
	panelselected = GameObject.Find("Camera").camera.GetComponent<PanelSelector>();
	}

	public void OnClick(){
	    if (var1 == 1) {
		panelselected.selectpanel[var2].SetActive (true);			
		} else if (var1 == 2){
			panelselected.selectpanel[var2].SetActive (false);
					
    }

So basically on my buttons I have two variables. Var1, Var2. Var1=1, Var2=0 opens Panel0 and if Var1=1 and Var2=1 it opens up Panel1.

If you have any ideas, I’d appreciated your help heaps!

Kind regards

Adam

If what I understood from your code is right,this is what you want

public static PanelSelector panelselected;
public int var1 = 0;
public int var2 = 0;
private int lastactive = 0;

void Start () 
{
  panelselected = GameObject.Find("Camera").camera.GetComponent<PanelSelector>();
}

public void OnClick()
{
  if(var1==1 && var2==0)
  {
    //Open Pannel 0
    panelselected.selectpanel[0].SetActive (true);
    
    //Close Pannel 1
    panelselected.selectpanel[1].SetActive (false);
    
    //Close Pannel 2 here if u have one
    //panelselected.selectpanel[2].SetActive (false);
  }

  if(var1==1 && var2==1)
  {
    //Open Panel 1
    panelselected.selectpanel[0].SetActive (true);

    //Close panel 0
    panelselected.selectpanel[0].SetActive (false);

    //Close Panel 2 here if u have one
    //panelselected.selectpanel[2].SetActive (false);
  }

  //if(/*Condition for Panel 2*/)
  //{
    //Open Panel 2
   //panelselected.selectpanel[2].SetActive (true);

    //Close Panel 0
    //panelselected.selectpanel[0].SetActive (false);

    //Close Panel 1
    //panelselected.selectpanel[1].SetActive (false);
    //}
}

Good Luck

Hi, thanks for replying guys! Appreciate it so much.

@Spherical Cow

The panels are GameObjects I believe. Well I created a script called PanelHandler.cs with public GameObject panelselected; in it. I then attached it to my UI Camera ‘Camera’ and added the panels to the array.

@Druidverse

Thanks for the help, but I don’t think that is what I am exactly after right now.

@Sethuraj

Seth, thanks heaps for this. I got it working and it does what is intended. However, I have a fair few buttons and panels, about 10. This means I have to write in…

if(var1==1 && var2==0)
{

panelselected.selectpanel[0].SetActive (true);

panelselected.selectpanel[1].SetActive (false);

panelselected.selectpanel[2].SetActive (false);

panelselected.selectpanel[3].SetActive (false);

All the way to…

panelselected.selectpanel[9].SetActive (false);

I was wondering if there was a way to set a last panel variable. Like - if Var1==1 and Var2==1, panel[0] set active (true). Then lastactive=var2. Then the bit I get a bid befuddled on… somehow comparing last active so… if var2 != last active, set lastactive false and open the new var2…

Thanks again for your time guys.

Adam

I am getting close with: ` void Start ()
{
selectedpanel = GameObject.Find(“Camera”).camera.GetComponent();
}

public void OnClick(){
	if (var1 == 1) {
		if (lastactive != -1) {
			selectedpanel.panelselected[lastactive].SetActive(false);
			}
			if (var2 != lastactive){
				selectedpanel.panelselected[var2].SetActive (true);
						lastactive=var2;
					} else {
						lastactive = -1;
					}
					
				}
				else if(var1==2){
					selectedpanel.panelselected[var2].SetActive(false);
					
				}
			}

}`

This ‘kinda’ opens and closes things… though I get an Array is out of range error at the third ‘if’ down. Not 100% sure how to handle it.

Regards again

Adam

/edit

Ok, so with the above code. When Button 1 is pressed the first time it opens Panel 1 [array element 0], when I press Button 1 again it closes Panel 1. Same for Button 2, first click, opens Panel 2 [array element 1] and click two will close Panel 2. The problem occurs when Button 2 is clicked, it sets lastactive=1, then if I don’t close Button 2, but instead open Button 1 it sets lastactive=0 and doesn’t close Button 2 at all. Basically I’m trying to work out that if I press Button 1 only Panel 1 [array element 0] shows, Button 2 only Panel 2 [1] shows… so on and so forth, all the way to Button 10, Panel 10 [9].