So, I am making a game, and whenever you press tab an inventory should pop up. Right now its just some gray boxes and TMP. I have an empty GO, with two other empty GOs. Pockets and a backpack. On both it has the boxes and TMP as a child.
using System.Collections.Generic;
using UnityEngine;
public class InventorySys : MonoBehaviour
{
//getting the player, and the OV_playerData
[SerializeField] private GameObject player;
private OV_PlayerData playerData;
//Backpack Bool
private bool bag;
//Getting the inventory
[SerializeField] private GameObject Inventory;
[SerializeField] private GameObject backpack;
//Open inventory bool (useful for the backpack to be showing only when the inventory is open)
private bool OpenInv;
// Start is called before the first frame update
void Start()
{
playerData = player.GetComponent<OV_PlayerData>();
bag = playerData.Backpack;
//making sure that the inventory is set to false
Inventory.SetActive(false);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Tab))
{
Inventory.SetActive(true);
OpenInv = true;
}
while (OpenInv == true)
{
if (bag == true)
{
backpack.SetActive(true);
}
}
}
}
After I run the game it works normally until I press tab when it just freezes.
CodePudding user response:
You have no exit condition for your while loop.
You don't need a while loop to make sure something is visible constantly, when a game object is active its going to be rendered until you set it inactive again.
using System.Collections.Generic;
using UnityEngine;
public class InventorySys : MonoBehaviour
{
//getting the player, and the OV_playerData
[SerializeField] private GameObject player;
private OV_PlayerData playerData;
//Backpack Bool
private bool bag;
//Getting the inventory
[SerializeField] private GameObject Inventory;
[SerializeField] private GameObject backpack;
//Open inventory bool (useful for the backpack to be showing only when the inventory is open)
private bool OpenInv;
// Start is called before the first frame update
void Start()
{
playerData = player.GetComponent<OV_PlayerData>();
bag = playerData.Backpack;
//making sure that the inventory is set to false
Inventory.SetActive(false);
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Tab))
{
OpenInv = true;
Inventory.SetActive(true);
if (bag == true)
{
backpack.SetActive(true);
}
}
}
}