Search Unity

Question Button onClick.AddListener definition not found

Discussion in 'Scripting' started by Gorjeck, Jul 30, 2020.

  1. Gorjeck

    Gorjeck

    Joined:
    Jun 22, 2020
    Posts:
    2
    Hello,

    I'm having my own librarry that I'm use in my unity project. Im referencing both UnityEngine end UI dll :
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    I'm trying to implement some UI element from this library :
    Code (CSharp):
    1. GameObject startButton = GameObject.Find("StartGameButton");
    2. startButton.GetComponent<Button>().onClick.AddListener(MyFunction);
    And I'm getting the following error :
    Note that this code work in my unity project.

    Thanks
     
  2. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    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.
     
  3. Gorjeck

    Gorjeck

    Joined:
    Jun 22, 2020
    Posts:
    2
    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.
     
  4. Cyber-Dog

    Cyber-Dog

    Joined:
    Sep 12, 2018
    Posts:
    352
    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.
     
  5. silentslack

    silentslack

    Joined:
    Apr 5, 2013
    Posts:
    393
    @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?
     
  6. silentslack

    silentslack

    Joined:
    Apr 5, 2013
    Posts:
    393
    The solution was not not use the larger UnityEngine.dll but instead the smaller one plus the individual Modules required.
     
  7. MartinMa_

    MartinMa_

    Joined:
    Jan 3, 2021
    Posts:
    455
    Code (CSharp):
    1. startButton.onClick.AddListener(MyFunction);