- Home /
ui panel disappears after showing for few seconds ?(problem)
i have a endless game , balls spawn endlessly and when wrong collision happens it shows a game over panel , the panel shows just fine but it doesnt last , it only shows for like 1 or 2 seconds then disappears
Environment : ball script which is attached to the ball prefab
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; using GoogleMobileAds.Api;
public class ball : MonoBehaviour { public static ball instance; public string currentcolor; public SpriteRenderer sr; public int targetscore = 10; public Color colorred; public Color coloryellow; public Color colorblue; public AudioSource collsound; float speed = 7; public GameOver_s gameOver; public float score; public GameManager CurrentScore; //-------------- public GameObject Panel; //----------- public GameObject BallSpawner ; // private bool isDead = false; private float StartTime;
//----------------
// Start is called before the first frame update
void Start()
{
hidePanel();
collsound = GetComponent<AudioSource>();
SetRandomColor();
}
void Update()
{
transform.Translate(Vector3.down * speed * Time.deltaTime);
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "ball")
{
gameObject.GetComponent<AudioSource>().Play();
Debug.Log(currentcolor);
}
if (col.tag == "colorchanger")
{
SetRandomColor();
return;
}
if (col.tag != currentcolor)
{
Destroy(GameObject.Find("circle"));
Destroy(GameObject.Find("circle (1)"));
Destroy(GameObject.Find("circle (2)"));
ShowPanel();
return;
}
GameManager.singleton.AddScore(1);
}
public void OnAdPanelExited () {
SceneManager.LoadScene("GameOver");
}
void SetRandomColor()
{
int index = Random.Range(0, 3);
switch (index)
{
case 0:
currentcolor = "yellow";
sr.color = coloryellow;
break;
case 1:
currentcolor = "red";
sr.color = colorred;
break;
case 2:
currentcolor = "blue";
sr.color = colorblue;
break;
}
}
public void ShowPanel()
{
AdPanelManager.instance.DisplayPanel();
}
public void hidePanel()
{
AdPanelManager.instance.HidePanel();
}
} and here is the one attached to the panel : using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class AdPanelManager : MonoBehaviour {
public GameObject Panel;
public static AdPanelManager instance;
private void Awake()
{
//Check if instance already exists
if (instance == null)
instance = this;
else if (instance != this)
Destroy(gameObject);
DontDestroyOnLoad(gameObject);
}
// Start is called before the first frame update
void Start()
{
//Panel = GameObject.Find("ADPanel_Holder");
HidePanel();
}
public void DisplayPanel()
{
Panel.SetActive(true);
//DontDestroyOnLoad(gameObject);
}
public void HidePanel()
{
Panel.SetActive(false);
}
}
Answer by oTaijjo · Dec 13, 2019 at 08:15 AM
Can't find anything wrong with the code. I suspect it's related to the setup of your scene. You could check the following:
Awake(), Start() etc. are NOT called when the scene is loaded, if the respective GameObject is not active. You have a hidePanel() in a Start() method there somewhere, maybe this is a call order issue?
If you're using Uniy UI (Canvas, Images'n stuff) check if you show some UI element with a non 0 z coordinate. I recently ran into the issue of the canvas disappearing, when I put a wrong number into ONE UI element's z coordinate.
Check if OnTriggerEnter2D() is called more often than expected. Maybe it screws with things.
That's all I could come up with. Good luck!
thank you , exactly what i needed , was adding the hide panel function in void start , and removing that function fixed it !