Home > Software design >  Menu panels show in game by default as open
Menu panels show in game by default as open

Time:07-27

i'm making a 2D game and my Menu panels are showing when i press play on unity they show as opened is there anyway to make them closed and only show when i press on menu button. this is the picture from the game:

menu open

And this is the script for panel opener when i press the menu button:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PanelOpener : MonoBehaviour
{

    public GameObject Panel;


    public void OpenPanle()
    {

        if (Panel != null)
        {

            bool isActive = Panel.activeSelf;
            Panel.SetActive(!isActive);

        }
    }
}

CodePudding user response:

Disable panels at start with a:

    private void Awake(){
        Panel.SetActive(false);
    }

Complete code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PanelOpener : MonoBehaviour
{

    public GameObject Panel;

    private void Awake(){
        Panel.SetActive(false);
    }

    public void OpenPanle()
    {

        if (Panel != null)
        {

            bool isActive = Panel.activeSelf;
            Panel.SetActive(!isActive);

        }
    }
}

Get yourself familiar with MonoBehaviour functions like Start, Awake, Update, FixedUpdate in the future.

  • Related