Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    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,787
    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:
    344
unityunity