- Home /
Other
FindGameObjectsWithTag only returning an array with one object?
So I've been trying to figure out a problem with my code in a previous post and my finding is that the GameObject[] I've created with FindGameObjectsWithTag only has a single GameObject in it at a time.
How can I get all the instantiated and tagged objects so I can then use their transform positions?
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour {
GameObject[] mountainTiles;
bool debugTime = true;
void Start(){
}
IEnumerator MountainDebug (){
yield return new WaitForSeconds (5.0f);
}
void FixedUpdate () {
PlayerMovement ();
}
void MountainChecker(){
mountainTiles = GameObject.FindGameObjectsWithTag("Tile3");
foreach (GameObject mountainTile in mountainTiles) {
if(debugTime){
Debug.Log (mountainTile.transform.position);
}
debugTime = false;
}
}
void PlayerMovement() {
StartCoroutine (MountainDebug());
MountainChecker ();
}
}
That would rather suggest that you only have one active gameobject in your scene hierarchy with the tag "Tile3". Did you expect more?
There are many more, in fact before instantiating there are none but after instantiating there are about 8 or so.
Answer by EpsilonQoppa · Feb 14, 2017 at 03:20 AM
List<GameObject> m_MountainTiles = new List<GameObject>();
Foreach(GameObject o in GameObject.FindGameObjectsWithTag("Tile3"))
m_MountainTiles.Add(o);
Lists would be okay, but I need to access the GameObject.Transform.position. I actually tried this suggestion to make sure it didn't work, even though I already knew lists were representations of objects and didn't contain their actual values.
This code works as well - so I've up voted it. Using a list implementation could also provide what you need but from your answer seems like you've perhaps not had experience using them.
In any case, as I said before - your issue isn't the code searching for the tags.
If you still believe it is, upload a small video showing that the objects you are searching for have the tags on the objects, when the search is being triggered.
Answer by DSebJ · Feb 14, 2017 at 01:28 AM
Your code works fine. I made a very small test class to check it with instantiated objects below.
I suspect the object your instantiating doesn't actually have the tag when it's instantiated. Check the new objects in the inspector to confirm ( you may not have updated the prefab's tag (which is how I was able to replicate your problem).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CountTags : MonoBehaviour {
GameObject[] mountainTiles;
void Update () {
MountainChecker();
}
void MountainChecker()
{
mountainTiles = GameObject.FindGameObjectsWithTag("Tile3");
foreach (GameObject mountainTile in mountainTiles)
{
Debug.Log(mountainTile.transform.position);
}
}
}
Perfect! I was beginning to think no one cared. Though, this makes a few hurdles for me to overcome now: I have to combine two of my scripts together...
EDIT: The issue isn't that the objects did not have the tag. They ARE getting the tag AFTER instantiation but that really shouldn't matter in the update script. I'll try changing it to during instantiation but I doubt your answer was sufficient, sorry.
I built a test project, instantiated a bunch of objects with this and found them. So the issue is either with your project setup or the ti$$anonymous$$g of the way your script is being called i.e. Before the tag is set. The problem is not the code searching for the objects. As per the script I attached.
Well then I'm at a loss, because the Objects are Instantiated on Awake and the Tags are assigned correctly OnEnable while the search script is trying to find them on a fixed update some time later. Furthermore it's unusual that it just returns the 1 and not any of the others: this one isn't even the prefab, it's a random Instance.
I can't even make a shitty tile-based videogame. I've got no job, no life, and no future. I might as well just fucking end it and stop wasting air.