Gamasutra is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Gamasutra: The Art & Business of Making Gamesspacer
View All     RSS
October 18, 2019
arrowPress Releases







If you enjoy reading this site, you might also want to check out these UBM Tech sites:


 

Finding Missing Object References in Unity

by Lior Tal on 12/08/14 01:44:00 pm   Featured Blogs

The following blog post, unless otherwise noted, was written by a member of Gamasutra’s community.
The thoughts and opinions expressed are those of the writer and not Gamasutra or its parent company.

 

Screen Shot 2014-12-06 at 15.54.40This article was originally posted on my blog at http://www.tallior.com

The post provides¬†a short solution for finding ‚ÄúMissing‚ÄĚ object references.

I plan to have more of these code posts that solve a specific problem I have faced myself; hopefully these could assist others and save them valuable time. Check out the blog for more of these articles in the future !

Missing References

A missing reference is different from no reference at all (in which case the inspector shows ‚ÄúNone‚ÄĚ). These are probably the result of some mixup in .meta files that results in having a link with an invalid object id. The problem is, for a large project it may be difficult to spot all these. Luckily, we can exercise some editor scripting to the rescue‚Ķ

Solution

Here is the full code for finding these missing references, in case you run into this issue:

[MenuItem("Tools/Find Missing references in scene")]
public static void FindMissingReferences()
{
    var objects = GameObject.FindObjectsOfType();
 
    foreach (var go in objects)
    {
        var components = go.GetComponents();
 
        foreach (var c in components)
        {
            SerializedObject so = new SerializedObject(c);
            var sp = so.GetIterator();
 
            while (sp.NextVisible(true))
            {
                if (sp.propertyType == SerializedPropertyType.ObjectReference)
                {
                    if (sp.objectReferenceValue == null && sp.objectReferenceInstanceIDValue != 0)
                    {
                        ShowError(FullObjectPath(go), sp.name);
                    }
                }
            }
        }
    }
}
 
private static void ShowError (string objectName, string propertyName)
{
    Debug.LogError("Missing reference found in: " + objectName + ", Property : " + propertyName);
}
 
private static string FullObjectPath(GameObject go)
{
    return go.transform.parent == null ? go.name : FullObjectPath(go.transform.parent.gameObject) + "/" + go.name;
}

 

To use, paste this code into a new class in an¬†editor folder,¬†load the scene you‚Äôd like to find missing references in, and click the menu option¬†‚ÄúTools/Find Missing references in scene‚ÄĚ.

Any issues found will be shown in the Console (as errors).


Related Jobs

University of Exeter
University of Exeter — Exeter, England, United Kingdom
[10.18.19]

Serious Games Developer
Square Enix Co., Ltd.
Square Enix Co., Ltd. — Tokyo, Japan
[10.17.19]

Experienced Game Developer
Wargaming Sydney
Wargaming Sydney — Sydney, New South Wales, Australia
[10.17.19]

Senior C++ Engineer (Gameplay Programmer)
Wargaming Sydney
Wargaming Sydney — Sydney, New South Wales, Australia
[10.17.19]

Gameplay Programmer, C++ - Vehicle Physics





Loading Comments

loader image