Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

248
Views
¿Cómo verificar si algún hijo de un objeto está activo?

He creado armas en mi juego e hice que las armas no estén activas cuando se toman, pero ahora el jugador puede tomar 2 armas al mismo tiempo. He agregado todas mis armas a un objeto vacío y quiero verificar si algún elemento secundario del objeto está activo. Todas las armas tienen el mismo script, pero los valores de los valores booleanos son diferentes. el metodo es asi

 void OnMouseDown() { if(weapon_is_taken == false) { weapon_is_taken = true; } }
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Hay varias formas dependiendo un poco de tus necesidades.

Para responder a su título, podría usar, por ejemplo,

 public bool IsAnyChildActive() { // Iterates through all direct childs of this object foreach(Transform child in transform) { if(child.gameObject.activeSelf) return true; } return false; }

Sin embargo, dependiendo de la cantidad de niños, esto puede ser un poco complicado cada vez.


Supongo que su objetivo es poder cambiar el arma activa e inmediatamente configurar todas las demás armas en inactivas.

Para esto, simplemente puede almacenar la referencia activa actual en su clase de Arma como, por ejemplo

 public class Weapon : MonoBehaviour { // Stores the reference to the currently active weapon private static Weapon currentlyActiveWeapon; // Read-only access from the outside // Only this class can change the value public static Weapon CurrentlyActiveWeapon { get => currentlyActiveWeapon; private set { if(currentlyActiveWeapon == value) { // Already the same reference -> nothing to do return; } // Is there a current weapon at all? if(currentlyActiveWeapon) { // Set the current weapon inactive currentlyActiveWeapon.gameObject.SetActive(false); } // Store the assigned value as the new active weapon currentlyActiveWeapon = value; // And set it active currentlyActiveWeapon.gameObject.SetActive(true); } } // Check if this is the currently active weapon public bool weapon_is_taken => currentlyActiveWeapon == this; public void SetThisWeaponActive() { CurrentlyActiveWeapon = this; } }
over 4 years ago · Santiago Trujillo Report

0

Gameobject en este contexto es un objeto principal que contiene objetos secundarios (armas)

 for (int i = 0; i < gameObject.transform.childCount; i++) { if (transform.GetChild(i).gameObject.activeSelf) { // do whatever you want if child is active } else { // do whatever you want if child is inactive } }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!