0
\$\begingroup\$

I am fixing an issue in a unity code that I have inherited. I have found the code file but need to fix the Canvas that is attached to that script.

Is there a way to determine, and load, the Canvas for the code file that I have found.

This is part of a vuforia enabled app. Here is the start of the file for the Canvas that I"m looking for

using UnityEngine; using System.Collections; using Vuforia;

public class InitErrorHandler : MonoBehaviour {
    #region PUBLIC_MEMBER_VARIABLES
    public UnityEngine.UI.Text errorText;
    #endregion //PUBLIC_MEMBER_VARABLES


    #region PRIVATE_MEMBER_VARIABLES
    private Canvas errorCanvas;
    private string key;
    #endregion //PRIVATE_MEMBER_VARIABLES


    #region MONOBEHAVIOUR_METHODS
    void Awake () 
    {
        // Get the UI Canvas that contains (parent of) the error text box
        if (errorText)
        {
            errorCanvas = errorText.GetComponentsInParent<Canvas>(true)[0];
        }

        VuforiaRuntime.Instance.RegisterVuforiaInitErrorCallback(OnInitError);
    }
    #endregion //MONOBEHAVIOUR_METHODS
...
}
\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

One of the fastest ways would be to add a Start script like so:

void Start () {
    string path = gameObject.name;
    Transform parent = transform.parent;

    while(parent != null) {
        path = parent.gameObject.name + "/" + path;
        parent = parent.parent;
    }

    Debug.Log('Here I am!\n" + path);
}

Run your project and wait for the "Here I am!" message to spring up in the console, printing out the path where you can find the object in the currently open scenes.

Obviously this only works if the script is used somewhere in an easily-reproducible execution path. If it's in a prefab or scene that's not in the normal flow, you won't see it this way, and we need to investigate more deeply...


First, make sure you've set your asset serialization mode to Force Text, so we have human-readable data to look at for the next steps. Save your scenes/project.

Next, find the .meta file for the script you're looking at. (You might need to show hidden files to be able to find it)

At the top of that metadata file, you'll find a GUID, a long string of hexadecimal characters like

guid: a652b1795b650454a8270045af8e2a71

This ID is how Unity references the script internally. If the script is used in any prefabs or scenes, those files will contain this ID.

You can use a program like Notepad++ to search all files in your assets directory to find matches for this ID. That will let you identify which prefabs or scenes it's used in. You might need to scan up the parent tree to see which object it's connected to if there are multiple similar objects in the file.


The last case this might miss is where the script is attached by another script at runtime, and isn't referenced in a scene/prefab.

You can check for this by right-clicking on the name of the class and selecting "Find References" to get a list of all places the class is mentioned in another script (like in an AddComponent<T> call)

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .