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

318
Vistas
Leaving one gameObject, deactivate others from an array

I have an array of gameObjects which I can deactivate easily with a loop. However I do not want to deactivate one gameObject from the lot. So I am providing an int value which is the position of the gameObject in that array. How do I deactivate others except for this particular object?

public GameObject[] myObjs;
int exceptionObj;

void Start()
{
  exceptionObj = 2; //Position of object in the array
  for(int i = 0; i<myObjs.Length; i++)
   {
     myObjs[i].SetActive(false);
   }
}
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

public GameObject[] myObjs;
int exceptionObj;

void Start()
{
  exceptionObj = 2; //Position of object in the array
  for(int i = 0; i<myObjs.Length; i++)
   {
     if(exceptionObj != i)
        myObjs[i].SetActive(false);
   }
}

Or;

public GameObject[] myObjs;
int exceptionObj;

void Start()
{
  exceptionObj = 2; //Position of object in the array
  for(int i = 0; i<myObjs.Length; i++)
   {
     if(exceptionObj == i)
        continue;

     myObjs[i].SetActive(false);
   }
}

Or perhaps even by simply reactivating at the end;

public GameObject[] myObjs;
int exceptionObj;

void Start()
{
  exceptionObj = 2; //Position of object in the array
  for(int i = 0; i<myObjs.Length; i++)
   {
     myObjs[i].SetActive(false);
   }
  myObjs[exceptionObj].SetActive(true);
}

over 4 years ago · Santiago Trujillo Denunciar

0

Two ways:

  1. With an if:

    exceptionObj = 2; //Position of object in the array
    for(int i = 0; i<myObjs.Length; i++)
    {
      if (i != exceptionObj) // If not the exception ID
          myObjs[i].SetActive(false);
    }
    
  2. Or with two loops:

    exceptionObj = 2; //Position of object in the array
    for(int i = 0; i<exceptionObj; i++)
    {
       myObjs[i].SetActive(false);
    }
    for(int i = exceptionObj+1; i<myObjs.Length; i++)
    {
       myObjs[i].SetActive(false);
    }
    
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