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

Assigning public static function to onClick button UI

Discussion in 'Scripting' started by Lostrick, Sep 14, 2015.

  1. Lostrick

    Lostrick

    Joined:
    Jan 9, 2014
    Posts:
    32
    I tried to call a public static function from my script but after selecting in the editor combo box it always said that the function is missing



    is it possible to call a public static function from a UI button?
     
  2. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    I do not believe the inspector allows configuring static methods, no.

    You can add them in code, since really the 'AddListener' method just takes a delegate.

    But the inspector is reflecting out that information, and probably only accepts instance scoped methods. Which would make sense, since you're referencing an instance, and not a class.
     
  3. Untherow

    Untherow

    Joined:
    Jun 19, 2016
    Posts:
    8
    I found a work around to this that kind of simulates static methods:

    Add your script onto a gameobject and make it a prefab. Now you can select the prefab in your button onclick and it will work just fine but you still need to use non static methods.

    I really don't understand though why, even today, those static functions are not supported. Would seem like a no-brainer to me.
     
    Last edited: Jun 7, 2018
  4. Lostrick

    Lostrick

    Joined:
    Jan 9, 2014
    Posts:
    32
    thank you very much Untherow, i've always been making 2 of the same function cause of this
    I didn't know we can call function from a prefab, this will really help
    you solved my 3 years problem lol
     
  5. nizmym

    nizmym

    Joined:
    Feb 27, 2019
    Posts:
    3
    Last edited: May 18, 2019
  6. e5me

    e5me

    Joined:
    May 22, 2019
    Posts:
    1
    I like static methods for things like settings. Because Onclick() only accepts public methods, I have a static class for a class. You might want to use singleton for this though. You can use this along with the prefab method Untherow suggested.
    ex)

    public class LevelManager{
    public static LevelManger levelManger;
    void Start(){
    if(levelManager){
    Destroy(this);​
    }
    else{
    levelManager = this;​
    }​
    }

    public void LoadLevel(){ .... }​
    }

    Then you can use loadlevel in other scripts as
    LevelManager.levelManager.LoadLevel();
    and if you attatch the levelmanager script to a prefab, you can use Onclick() to call loadlevel without having an object in the hierarchy. I hope it helps to the future googlers
     
  7. vfxjex

    vfxjex

    Joined:
    Jun 3, 2013
    Posts:
    93
    This works like a charm... Thanks!
     
  8. Sparky1027

    Sparky1027

    Joined:
    Oct 22, 2018
    Posts:
    1
    Just to add to e5me's response. If your prefab is a prefab that has the DontDestroyOnLoad method in it (think of Brackey's AudioManager) yet you still want to call some methods from the Inspector and the Event Triggers, you can modify the prefab's methods to check if the method being called is from the original instance or a new one. If it's not the original, call the instance.

    Code (CSharp):
    1. public class LevelManager
    2. {
    3.     public static LevelManger instance;
    4.  
    5.     private void Start()
    6.     {
    7.         if (levelManager)
    8.             Destroy(this);
    9.         else
    10.             levelManager = this;
    11.     }
    12.  
    13.     public void LoadLevel()
    14.     {
    15.         if (instance == this)
    16.         {
    17.             // Do original code
    18.         }
    19.         else
    20.             instance.LoadLevel();
    21.     }
    22. }
    Afterwards, have your button.hover or button.click call the prefab's public method. It will use the instance instead of the original to avoid any NullReferenceExceptions you may have from using the wrong instance from the UI.