Button onClick.AddListener definition not found

Hello,

I’m having my own librarry that I’m use in my unity project. Im referencing both UnityEngine end UI dll :

using UnityEngine;
using UnityEngine.UI;

I’m trying to implement some UI element from this library :

GameObject startButton = GameObject.Find("StartGameButton");
startButton.GetComponent<Button>().onClick.AddListener(MyFunction);

And I’m getting the following error :

Note that this code work in my unity project.

Thanks

Hey,

How is your library setup? Have you tried calling these functions from a script inside your project?

I’m surprised you didn’t have issued building a library, every-time I have built a dll for Unity it has had to exclude anything Unity related and just work of the .Net framework.

Hi,

I’m referencing both UnityEngine.dll from /Applications/Unity/Hub/Editor/2020.1.0f1
And UnityEngine.UI from my unity app which is /path/to/app/Library/ScriptAssemblies/UnityEngine.UI.dll

I have actually have 2 libraries, one that I build and move the library directly to the Assets folder and one that I load after runtime using Assembly.LoadFrom(“path/to/dll”);

Both work but now I have this problem.

I always had issues trying to build a library reliant on Unity. These days if its a library I am building, I will keep it to just dot net framework.

Sorry, that’s not an answer to your problem. But tbh, your better of just creating scripts.

Is its because your trying to make an asset for the unity store? If that’s the case, its another reason to not build with unity DLLs. Someone who purchases your asset would not be able to make any version change fixes.

@Gorjeck I’ve ran into the exact same issue with Unity 2020.3.7f1. I’m including the UnityEngine.dll from Hub/Unity into my csproj dependency list but for some reason UnityEvent is not being found inside UnityEngine.dll even though it should be in the UnityEngine.CoreModule…

Did you find a solution for this?

The solution was not not use the larger UnityEngine.dll but instead the smaller one plus the individual Modules required.

startButton.onClick.AddListener(MyFunction);