- Home /
Find tagged children within a specific, dynamically loaded in Prefab gameObject
The more I look the more frustrated I keep getting. At the bottom I've linked the previous answers that haven't worked for me. I'm still fairly new to Unity so I apologize if the answer is obvious.
I'm creating a puzzle game and dynamically loading in a few Prefab "levels" with gameObjects that are tagged a few layers deep in a hierarchy. I'm trying to find the gameObjects with the "piece" tag only within the Prefab that is loaded as the current level. When I try the code below, the response keeps coming back as null.
The actual gameObject hierarchy looks something like this:
| LevelLoader
|| CurrentLevel
||| Level01 (dynamically loaded in Prefab)
|||| Pieces
||||| A0, A1, etc (gameObjects with "piece" tag)
And the code looks like this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PieceManager : MonoBehaviour {
public static int piecesNeededForVictory;
public static int currentPiecesSuccessfullyPlaced;
public static List<GameObject> piecesInScene = new List<GameObject>();
public GameObject _currentLevel;
public Component[] pieceTags;
void Start () {
_currentLevel = LevelSpawner.CurrentLevelContainer;
if (_currentLevel != null)
{
pieceTags = _currentLevel.GetComponentsInChildren<Transform>();
foreach (Transform child in pieceTags)
piecesInScene.Add(child.gameObject);
PrintArrayList("Here are the pieces in the current level...", piecesInScene);
}
if (piecesInScene.Count == 0)
{
Debug.Log("WARNING: No game objects are tagged with 'piece'.");
}
piecesNeededForVictory = piecesInScene.Count;
currentPiecesSuccessfullyPlaced = 0;
}
}
Here are the previous solutions I've tried:
Is it possible to FindWithTag only within children of a certain gameObject?
How do I find a child object in a hierarchy of children?
Your answer
Follow this Question
Related Questions
Calculating BoxCollider bounds and center in runtime 1 Answer
How to instantiate several weapons with references to other child of prefabs? 0 Answers
how to detect that 3 cube in the box are from the same tag 1 Answer
Rotating/Aligning parents relative to their children 1 Answer
How to make multiple gameobjects child of one and the same transform (SCRIPT) 2 Answers