Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

434
Vistas
How to check if the gameObject is active or not

I am making a backpack and I am trying to make it like Minecraft how you just press "E" and it will open and if is already open then they close it. Bty I am using Unity in C#.

if (Input.GetKeyDown(backpackKey) && backpack.activeInHierarchy == false)
{
   backpack.SetActive(true);
}
if (Input.GetKeyDown(backpackKey) && backpack.activeInHierarchy == true)
{
    backpack.SetActive(false);
}
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Both your if conditions will match:

  1. your object is inactive
  2. You set it active
  3. Now also the second condition matches
  4. You set it inactive again

Either use else if to make both blocks run exclusive or .. actually you could simply do

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);
}
over 4 years ago · Santiago Trujillo Denunciar

0

You are missing an else statement:

if (Input.GetKeyDown(backpackKey))
{
  if(backpack.activeInHierarchy == false)
  {
    backpack.SetActive(true);
  } else {
    backpack.SetActive(false);
  }
}

In your code, whenever you try to open it then you end up falling into the second condition, now true, and closing it immediately.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda