Menu panels show in game by default as open - c#

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:
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);
}
}
}

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

Unity script work on Editor but not working in Android

Hello I'm game developer one day when I want to test my game on android I join to my game but when I press on buttons public void function doesn't work I fixed it by add to scripts what using buttons onClick.AddListener() but one problem IAP don't work with onClick.AddListener() you can help me fix problem with public void function what works on Editor but don't work in Android when I press on buttons. Thanks.
Script with AddListener [This script where I add onClick.AddListener() to fix this problem but it's be uncomfortable thats why I want to fix this]:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class GameButtonManager : MonoBehaviour
{
public GameObject DonationShop;
public GameObject ShopBtn;
public GameObject QuitShopBtn;
public GameObject QuitBtn;
public bool CanPress = true;
void Update()
{
ShopBtn.GetComponent<Button>().onClick.AddListener(EnableShop);
QuitShopBtn.GetComponent<Button>().onClick.AddListener(DisableShop);
QuitBtn.GetComponent<Button>().onClick.AddListener(BackToMenu);
}
public void EnableShop()
{
if(CanPress == true)
{
DonationShop.SetActive(true);
CanPress = false;
StartCoroutine(CanPressEnable());
}
}
public void BackToMenu()
{
SceneManager.LoadScene(1);
}
public IEnumerator CanPressEnable()
{
yield return new WaitForSeconds(5.5f);
CanPress = true;
}
public void DisableShop()
{
if(CanPress == true)
{
DonationShop.SetActive(false);
CanPress = false;
StartCoroutine(CanPressEnable());
}
}
}

How to load death scene in C#/unitygames?

haven't posted on here before but I have been trying for a while to create a game and would like a death/game over sort of scene to appear when the player loses all 3 of their lives. I have a functioning game manager and my player can lose lives (they have 3). This is all being done in unity games and is 2d (idk if that helps). I currently have other stuff in my scene loader script that works fine so I will post the whole thing but I am having issues with the bottom most code!
Thank you!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneLoader : MonoBehaviour
{
public string scenename;
public GameManager GM;
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.tag == "Player")
{
SceneManager.LoadScene(scenename);
}
}
private void Deathscene()
{
if(GM.LifeTotal == 0)
{
SceneManager.LoadScene(Bob);
}
}
}
Gamemanager script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class GameManager : MonoBehaviour
{
public int PotionsCollected = 0;
public int LifeTotal = 3;
public Text PotionsOutput;
public Text LifeOutput;
void Update()
{
PotionsOutput.text = "Potions: " + PotionsCollected;
LifeOutput.text = "Life: " + LifeTotal;
}
public void CollectPotion()
{
PotionsCollected++;
}
public void UsePotion()
{
PotionsCollected--;
}
public void LoseLife()
{
LifeTotal--;
}
}
What you can do is from your Unity Editor go to File->Build Settings and then drag and drop inside the active scenes window your death scene.
Then an index will be generated on the right side of the window and you can use that index to load the scene. like this:
SceneManager.LoadScene("Use your generated index");
NOTE: Use your index as a number and not as a string.
UPDATED Solution:
public void LoseLife()
{
LifeTotal--;
if(LifeTotal <= 0)
{
SceneManager.LoadScene("Use your generated index");
}
}
I supposse LoseLife() it's called when the enemy attacks your player.

How to Destroy and Instantiate a GameObject?

I have a GameObject that I want to Destroy and Instantiate depending on button clicked. I don't really understand how it works but is it something like the code below? Then I attach the script on some gameobject then on onClick set via the function name.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CreateDestroyAR : MonoBehaviour {
//PAGE HAS BEEN CREATED AS A PREFAB
public GameObject Page;
public void CreatePage() {
Instantiate(Page);
}
public void DestroyPage()
{
Destroy(Page);
}
}
Yes, they are "pages" which I can just use setActive for but one of the "page" needs to be destroyed when a different button is clicked and re-instantiated every time its corresponding button is clicked.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CreateDestroyAR : MonoBehaviour {
//PAGE HAS BEEN CREATED AS A PREFAB
public GameObject Page;
private GameObject instantiatedPage;
public void CreatePage() {
instantiatedPage = Instantiate(Page);
}
public void DestroyPage()
{
Destroy(instantiatedPage);
}
}

Button and PlayerPrefs

i have a code i want when i click button not set active (with PlayerPrefs) but problem when i run game a button not set active before i click it (button)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class fcfg : MonoBehaviour
{
public GameObject butt;
void Start()
{
if (PlayerPrefs.GetInt("ButtonActivate") == 0)
{
butt.gameObject.SetActive(false);
}
else if (PlayerPrefs.GetInt("ButtonActivate") == 1)
{
butt.gameObject.SetActive(true);
}
}
void Update()
{
}
public void whenbuttonclick()
{
this.gameObject.SetActive(false);
PlayerPrefs.SetInt("ButtonActivate", 0);
}
}

Unity Button dont want to close a canvas

Hello i made a canvas in unity.
when the player preses the Esc button a canvas opens
In the canvas i made a button when the player clicks on the button the
Canvas need to close but nothing happens when i press the button.
Can somebody help me with this problem?
The Canvas
using UnityEngine;
using System.Collections;
using System;
using UnityEngine.SceneManagement;
public class PauseMenu : MonoBehaviour
{
private Canvas PlayerPauseMenu;
// Use this for initialization
void Start()
{
PlayerPauseMenu = GetComponentInChildren<Canvas>();
}
// Update is called once per frame
void FixedUpdate()
{
HandleInput();
}
private void HandleInput()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
PlayerPauseMenu.enabled = true;
}
}
public void CloseCanvas()
{
PlayerPauseMenu.enabled = false;
}
}

Categories