Why doesn't my script print all the objects in the scene?

It’s a really simple script and it should work

using UnityEngine;
using System.Collections;

public class ObjectsGenerator : MonoBehaviour {
    public GameObject[] gos;
    // Use this for initialization
    void Start () {
        gos = GameObject.FindGameObjectsWithTag("Untagged");
        for (int i = 0; i < gos.Length; i++) {
            Debug.Log (gos);
        }
    }
}

Strangely enough, while gameObject.tag == “Untagged” works, GameObject.FindGameObjectsWithTag(“Untagged”) doesn’t.

You can get around by this by getting all the GameObjects in the scenes, and verify their tag.

var gameObjects = GameObject.FindObjectsOfType<GameObject>();

foreach(GameObject go in gameObjects)
{
    if (go.tag == "Untagged")
    {
        Debug.Log (go.name);
    }
}
1 Like

Thank you, that is indeed strange!

I’ll go out on a limb and say untangled is handled internally as a null instead of as a string.

I could be completely wrong, just making a guess.

I share the same guess as you :slight_smile:

Thanks, @FaithRaven

but we should use Object.FindObjectsOfType(); because the method is in UnityEngine.Object not GameObject.