I have a game object called objects that contains 4 game objects. In code I am object pooling and moving all 4 objects at once and recycling them. objects[currentObject] is how new pairs are created and then recycled. If the 4 objects in a single game object are named Object1, Object2, Object3, Object4 how do I call one of those objects out of object so I may setActive(false)?
If i understood you well, are you working with Child-parent relationship?
If the answer is yes.
in your code you can do:
transform.GetChild(/*Number of the child*/);
If you are looking for a script in child gameObjects you could use GetComponentsInChildren<T>()
which will return an array of type T
, T
being your script. Then loop through that array and SetActive(false)
.
Otherwise if its not script or component sepecific and you just want the gameObjects use a for loop
for (int i = 0; i < transform.childCount; i++)
transform.GetChild(i).gameObject.SetActive(false);
or if you are looking for a specific object like a gameObject named “Object4” use transform.Find("Object4")
DO NOT PUT if (ThisCard.transform.parent.gameObject == ESlot1) FOR SOME REASON UNITY 2022 DOESNT UNDERSTAND THE PARENT OF THE GAMEOBJECT INSTEAD==>
public Transform ThisCard;
public Transform TSlot1;
public Transform ESlot1;
Card[] card1;
if (ThisCard.transform.parent == ESlot1)
{
card1 = TSlot1.GetComponentsInChildren<Card>();
card1[0].currentHealth -= damage;
Debug.Log("EnemyAttacked!!!");
}