Estoy haciendo una mochila y estoy tratando de hacerla como Minecraft, simplemente presiona "E" y se abrirá y, si ya está abierta, la cierran. Por cierto, estoy usando Unity en C#.
if (Input.GetKeyDown(backpackKey) && backpack.activeInHierarchy == false) { backpack.SetActive(true); } if (Input.GetKeyDown(backpackKey) && backpack.activeInHierarchy == true) { backpack.SetActive(false); }Ambas condiciones if coincidirán:
O use else if para hacer que ambos bloques se ejecuten de forma exclusiva o ... en realidad, simplemente podría hacer
if (Input.GetKeyDown(backpackKey)) { // I would prefer using "activeSelf" // If any parent is inactive your check might always return false even though the object // itself is already set to active. Don't know if it has an impact on performance // I just find it more convenient here since this is the value changed by "SetActive" backpack.SetActive(!backpack.activeSelf); }Te falta una declaración else :
if (Input.GetKeyDown(backpackKey)) { if(backpack.activeInHierarchy == false) { backpack.SetActive(true); } else { backpack.SetActive(false); } }En su código, cada vez que intenta abrirlo, termina cayendo en la segunda condición, ahora verdadera, y cerrándolo inmediatamente.