Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

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

Discussion in 'Scripting' started by Larpushka, Jul 19, 2015.

  1. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    It's a really simple script and it should work


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class ObjectsGenerator : MonoBehaviour {
    5.     public GameObject[] gos;
    6.     // Use this for initialization
    7.     void Start () {
    8.         gos = GameObject.FindGameObjectsWithTag("Untagged");
    9.         for (int i = 0; i < gos.Length; i++) {
    10.             Debug.Log (gos);
    11.         }
    12.     }
    13. }
    14.  
     
  2. FaithRaven

    FaithRaven

    Joined:
    Feb 19, 2015
    Posts:
    4
    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.

    Code (CSharp):
    1. var gameObjects = GameObject.FindObjectsOfType<GameObject>();
    2.  
    3. foreach(GameObject go in gameObjects)
    4. {
    5.     if (go.tag == "Untagged")
    6.     {
    7.         Debug.Log (go.name);
    8.     }
    9. }
     
    zwcloud likes this.
  3. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    Thank you, that is indeed strange!
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,847
    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.
     
  5. FaithRaven

    FaithRaven

    Joined:
    Feb 19, 2015
    Posts:
    4
    I share the same guess as you :)
     
  6. zwcloud

    zwcloud

    Joined:
    Mar 15, 2016
    Posts:
    363
unityunity