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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question Button OnClick missing reference (scene bug?) after switching to Assembly Definitions

Discussion in 'Editor & General Support' started by Skyunity, Nov 15, 2022.

  1. Skyunity

    Skyunity

    Joined:
    Nov 15, 2014
    Posts:
    5
    I recently converted my project to using Assembly Definitions. I was able to resolve all errors including some semi-hidden ones I could only see when deleting Library and loading the project fresh.

    However, now MANY of my OnClick button references display <Missing Class.FunctionName> while still showing the class reference, the parameter variable references, and even the function name!

    So for example, one of my Button OnClicks shows <Missing LobbyMenuManager.SwitchMenuPage> with LobbyMenuManager class instance set, an in-scene MenuPage class parameter assigned, and I can even click on the function reference and re-assign it there manually.

    However, there are hundreds of Button references scattered throughout 4 scenes, so manually correcting this would take a while and have potential for hard-to-debug user error. Is this a Unity bug? Is there some way to refresh the scene references (it seems like it still has them!).

    Any ideas on why this is happening or how to solve would be greatly appreciated!
     
  2. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,917
    No.

    UnityEvents uses reflection to serialise the method's fully qualified path. This will include it's current assembly, namespace and class name. I believe if any of these change then the unity event's serialised string will be invalid.

    The same thing happens with the SerializeReference attribute, which uses a reference types' fully qualified name to serialise a pointer to the class' type.

    There's an undocumented attribute called
    MovedFrom
    , which lives in the
    UnityEngine.Scripting.APIUpdating
    namespace that you can use fix your SerializeReference references. I have no idea if it works with UnityEvents, but it's worth a shot isn't it?
     
    Skyunity likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,780
    Personally I always strive to avoid this "Unity Way(tm)" of sprinkling random button / UI connections all throughout my scenes and code. Even without renaming or moving code around it is fragile and out-of-sight and becomes a nightmare to debug and maintain, getting worse with every passing day as you forget what you did originally.

    To this end I made a package (that I call Datasacks) that has a single generic "button intent" script that goes on every button, and then the actual decision about what to do is handled entirely in code.

    20180724_datasacks_overview.png

    Datasacks is presently hosted at these locations:

    https://bitbucket.org/kurtdekker/datasacks

    https://github.com/kurtdekker/datasacks

    https://gitlab.com/kurtdekker/datasacks

    https://sourceforge.net/projects/datasacks/
     
    Skyunity likes this.
  4. Skyunity

    Skyunity

    Joined:
    Nov 15, 2014
    Posts:
    5
    Figured out a solution, thanks for the posts they helped inspire the right search terms for me to find what I needed.

    Post-Mortem for if anyone else has this issue:
    Button UnityEvent Missing after Assembly Definition change


    The issue appeared when assembly definitions changed. Any in-scene UnityEngine.UI Buttons that had OnClick references referring to scripts moved into AgoraWorldCore had the potential for their persistent button events (m_ObjectArgumentAssemblyTypeName and m_TargetAssemblyTypeName) to have Assembly-CSharp rather than the new DLL reference. This also may affect Prefabs not in scenes, but it seemed to only be the prefabs in scenes for me.


    The issue was fixed by opening the Scene or Prefab file (creating a backup first), then finding usage of "Assembly-CSharp" and replacing it with "MyNewCoreDLLName". Scene/prefab files need to be turned on to human readable (text) in the Unity Preferences.


    Note, that if you have buttons referencing scripts in multiple new DLLs, you'll have to comb through all usage of Assembly-CSharp or buttons (m_ObjectArgumentAssemblyTypeName and m_TargetAssemblyTypeName) and determine which assembly to use. In our case, we essentially moved the bulk of our code to a Core DLL, with the other DLLs used only interacting with the code (no buttons).


    To prevent this issue in future, we would ideally adopt a paradigm of Button assignment that keeps all assignment in code (but realities of fast development what they are, we will just have to watch out for this issue in future and try to keep all Button assignment only to the core of our code, NO other DLL).
     
    Kurt-Dekker and spiney199 like this.