Forum rules - please read before posting.

Unity UI prefab menus - OnEnable/OnDisable called multiple times

edited November 2018 in Technical Q&A
I have custom menu item with a script attached that triggers an animation via SetTrigger. This happens in the OnEnable() function.
Now the problem is, that OnEnable gets called twice and with it, so does the OnDisable, causing the animator to reset the parameter since it got disabled and enabled once again.
The menu item has appear type set During gameplay, so this is probably the cause for this issue.

This is the relevant code from the script attached to the root object of the UI Element.

 private void OnEnable()
    {

        var a = AC.GlobalVariables.GetAllVars().Find(x => x.label == globalIntVar_State);
        Debug.Log(animator);

        if (a != null && a.IntegerValue == 2)
        {
            Debug.Log(a.IntegerValue);

            button.SetActive(true);
            GetComponent<Animator>().SetTrigger("new location unlocked");
            animator.SetTrigger("new location unlocked");
            Debug.Log(animator.enabled);
            a.IntegerValue = 1;
        }
    }

 Off: I'd like to point out that this forum has serious issues regarding the search feature and editing posts(bold does not work for example).

Comments

  • Do you have only one instance of this item? Seems like there's two scripts and thus it's running twice.
  • Please elaborate on the issues with search you're having.

    In order to prepare a Menu for display at runtime, it has to be momentarily enabled/disabled in a single frame at startup.  What you're likely seeing is the effect of that.

    What you can do instead is hook into the OnMenuTurnOn custom event, which kicks in when a Menu is turned on "properly", and not just part of the initalisation:


  • @Keks I checked and I have only one instance with one single script. Might as well be a Unity bug, because it couldn't replicate it again today. 

    @ChrisIceBox ; 1 or 2 weeks ago the search feature changed in a way that it now finds more literal results, like seen in this screenshot: http://prntscr.com/lmdp5o .
    I hardly find any usable results in comparison to before. The display of the results changed as well.

    I will use the from you suggested custom events, they seem perfect for this case, thanks!
  • edited November 2018
    yeah, i'd second what @11tomi12 said about the Search funtionality

    i thought maybe it was my old man's memory playing tricks on me, but i could have sworn that in the past you could put multiple search items into the box and get some sensible results (i.e. it assumed you were searching for "A + B"), but nowadays it gives you anything with A or B in it, which means it's practically impossible to find what you're searching for
  • @HarryOminous This, yes! Well explained. 
  • Before it's changed i recommend searching the forum via Google.
    "site:https://adventurecreator.org/forum/ search"
  • The forum software was recently upgraded for security, but the old Google search was lost.  I've re-implemented it now.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.