How to put ".transform" after "GameObject.FindGameObjectsWithTag()"
So I am trying to do this:
bush = GameObject.FindGameObjectWithTag("Bush").transform;
But with this:
bush = GameObject.FindGameObjectsWithTag("Bush").transform;
But for some reason, trying to find GameObject*s* with the tag and putting ".transform" after it won't work... I tried removing the ".transform" after it and putting a foreach statement after it, like so:
foreach (GameObject bushes in bush)
{
bushes.transform();
}
But it still doesn't work. So I came here, and would appreciate the help. :)
what do you mean by doesn't work? the latter is correct except for .transform(). is that an extension method you implemented for gameobjects? what should that line do?
Well it does work. The "GameObject.FindGameObjectWithTag("Bush").transform" works. But I need it to recognize Object"S". With an s after it. But it returns an array, so it can't have .transform after it.
Answer by Kiefy · Jan 03, 2017 at 09:37 PM
FindGameObjectsWithTag() returns an array of all objects found.
You would need to use for:
bushes = GameObject.FindGameObjectsWithTag( "Bush" );
for ( int i = 0; i < bushes.Length; i++ )
{
print( bushes[i].transform )
}
or foreach:
bushes = GameObject.FindGameObjectsWithTag( "Bush" );
foreach ( GameObject bush in bushes )
{
print( bush.transform )
}
or you can gamble on the first one by using:
bushes = GameObject.FindGameObjectsWithTag( "Bush" );
print( bushes[0].transform );
or even:
print( GameObject.FindGameObjectsWithTag( "Bush" )[0].transform )