- Home /
Turning a Panel on and off
Unity 2019.4.21f1
I have a VR app (Android 7 Device)that uses a raycast to open and close dialogue panels. I have one script that opens an instruction panel, and another to close it. Both scripts have a gaze timer built in. The problem is that the first time the panel opens fine. It closes fine the first time also. However, the second time i open the panel it opens briefly and then closes. I know that the issue is that once the close script is running, it continues to do so on update . However, having tried all sorts I cannot work out how to stop the close script until it is needed again. Any help would be gratefully received.
The Open script is attached to the Instruction button in the UI (note this is not a Unity button but an image with script attached)
The Close script is attached to the close button on the instruction panel. This is a Unity button.
Open Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class popUpInstOpen1 : MonoBehaviour
{
public GameObject InstructionPanel;
public Image gazeCircle;
public bool IsOpen = false;
private float totalTime = 2;
private bool gvrStatus = false;
private float gvrTimer = 0;
void Start()
{
gazeCircle.fillAmount = 0;
}
void Update()
{
if (gvrStatus)
{
gvrTimer += Time.deltaTime;
gazeCircle.fillAmount = gvrTimer / totalTime;
//Debug.Log("instructions open line 25");
}
if (gvrTimer > totalTime)
{
gazeCircle.fillAmount = 0;
InstructionPanel.SetActive(true);
gvrStatus = false;
gvrTimer = 0;
IsOpen = true;
Debug.Log("instructions open line 36");
}
}
public void OnPointerEnter()
{
gvrStatus = true;
Debug.Log("instructions onpointer enter gvr true line 42");
}
public void OnPointerExit()
{
gvrStatus = false;
gvrTimer = 0;
gazeCircle.fillAmount = 0;
Debug.Log("instructions onpointer exit gvr true line 49 - popupInstOpen");
}
}
Close Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class popupInstClose1 : MonoBehaviour
{
public GameObject InstructionPanel;
public GameObject InstructionButton;
public popUpInstOpen1 IsOpenCheck;
public Image gazeCircle;
private float totalTime = 2;
private bool gvrStatus = false;
private float gvrTimer = 0;
void Start()
{
gazeCircle.fillAmount = 0;
}
void Update()
{
IsOpenCheck = InstructionButton.GetComponent<popUpInstOpen1>();//this line accesses script on open button
Debug.Log(IsOpenCheck.IsOpen);
if (gvrStatus)
{
gvrTimer += Time.deltaTime;
gazeCircle.fillAmount = gvrTimer / totalTime;
}
if (gvrTimer > totalTime)
{
gazeCircle.fillAmount = 0;
Destroy(InstructionPanel);
InstructionPanel.SetActive(false);
if (IsOpenCheck.IsOpen = true)
{
InstructionPanel.SetActive(false);
Debug.Log("instructions close line 41");
IsOpenCheck.IsOpen = false;
Debug.Log(IsOpenCheck.IsOpen);
}
else
{
//keeps panel open
Debug.Log(IsOpenCheck.IsOpen);
InstructionPanel.SetActive(true);
Debug.Log("instructions panel stay open line 50");
}
}
}
public void OnPointerEnter()
{
gvrStatus = true;
}
public void OnPointerExit()
{
gvrStatus = false;
gvrTimer = 0;
gazeCircle.fillAmount = 0;
Debug.Log("instructions onpointer exit gvr true line 66 popupInstClose1");
}
}
Answer by HellsHand · Mar 25, 2021 at 08:13 PM
Line 38 of your close script, always evaluates to true, unless I missed something you should be using '==' instead of '='.
Your answer
Follow this Question
Related Questions
simple networked int script simply dont work. 1 Answer
Triggering GUI Button from collision with GameObject 2 Answers
Make a Button Behave Like a Toggle 5 Answers
show gui toggle when a button is pressed 0 Answers
Stuck with sliding GUI panels 0 Answers