- Home /
Question by
Brandalf · Apr 24, 2018 at 01:49 PM ·
arrayparentgameobjectsfindwithtagroot
Create an array of root game objects with the same tag
I want to create an array that is only root game objects with the tag "player"
GameObject[] players = GameObject.FindGameObjectsWithTag("Player");
Comment
Best Answer
Answer by Hellium · Apr 24, 2018 at 01:54 PM
// Add `using System.Collections.Generic;` at the top of your file
List<GameObject> players = new List<GameObject>( GameObject.FindGameObjectsWithTag("Player") ) ;
// Loop from end to start since we might remove elements
for( int i = players.Count - 1 ; i >= 0 ; --i )
if( players[i].transform.parent != null ) players.RemoveAt( i ) ;