Losing It: When Unity3D Is Unable To Find Its Objects in the Build Anymore

08 Sep 2010
Posted by xeophin

When preparing my game for Fantoche, I ran into problems several times, since upon building my game, the screen either remained black or parts of my game did not work, with the log showing a NullReferenceException.

In both times, it was related to me tagging objects in the Unity 3D editor. This is caused by the somewhat peculiar implementation of tagging in Unity which only allows one tag to be added to an object.

Pitfall No. 1: EditorOnly

Tagging objects in the editor as EditorOnly will make the game work in the editor – but not in the build, as those objects won't be included when building your game.

Pitfall No. 2: Trying to Access One of Several Objects with the Same Tag

When accessing GameObjects using the FindObjectsWithTag() function, make sure that only one object has this tag added.

The function returns the first object with this tag it encounters. While your game might work in your editor because by chance your script always gets the right object first, this order is overturned in the build, therefore likely to return a completely different object with that tag, and possibly breaking your game.

A Little Helper

In order to track down wrongly tagged objects, I have thrown together a little script.

Copy the following script, save it under the name TagFinder.cs, place it in your project folder and attach it to a game object in your scene; then add some tags you want to look for.

// xeophin.net/code
// 
// (c) 2010 Kaspar Manz
// code@xeophin.net
// 
// All rights reserved.
// 
using System;
using UnityEngine;
 
[AddComponentMenu("Helper Scripts/List Objects By Tag")]
 
/// <summary>
/// This is a simple little script that lists all objects with a
/// certain tag, in order to find wrongly tagged objects.
/// </summary>
public class TagFinder : MonoBehaviour {
 
    /// <summary>
    /// A list of tags to look for.
    /// </summary>
    public string[] tagsToFind = { "EditorOnly" };
 
    /// <summary>
    /// At the start of the game, this script lists all objects
    /// with the defined tags.
    /// </summary>
    void Start () {
        foreach (string tag in tagsToFind) {
            print ("Objects tagged with '" + tag + "':");
 
            try {
                GameObject[] objects = GameObject.FindGameObjectsWithTag (tag);
                if (objects.Length != 0) {
                    foreach (GameObject item in objects) {
                        print ("  " + item.name + " on layer " + item.layer);
                    }
                } else {
                    print ("  There are no objects with the tag '" + tag + "'.");
 
                }
            } catch (UnityException ex) {
                print ("  The tag '" + tag + "' has not been found. Exception Message: " + ex.Message);
            }
 
        }
 
    }
}

This should give you an overview over the objects that use certain tags – and might solve your problems in less hours than I used to solve mine ...

When preparing my game for Fantoche, I ran into problems several times, since upon building my game, the screen either remained black or parts of my game did not work, with the log showing a `NullReferenceException`.In both times, it was related to me tagging objects in the Unity 3D editor. This is caused by the somewhat peculiar implementation of tagging in Unity which only allows one tag to be added to an object.## Pitfall No. 1: `EditorOnly` ##Tagging objects in the editor as *EditorOnly* will make the game work in the editor – but not in the build, as those objects won't be included when building your game.## Pitfall No. 2: Trying to Access One of Several Objects with the Same Tag ##When accessing GameObjects using the `FindObjectsWithTag()` function, make sure that only **one** object has this tag added.The function returns the first object with this tag it encounters.

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
By submitting this form, you accept the Mollom privacy policy.


Navigation



Languages