Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug DropDown menu on value change nolonger work after loading in to other scene

Discussion in 'Editor & General Support' started by hiaron123, Sep 16, 2022.

  1. hiaron123

    hiaron123

    Joined:
    Jul 18, 2019
    Posts:
    28
    The dropdown menu works the first time I play But after switching scene it doesn't work. The switch scene basically loads the same scene again, however, the dropdown menu on value change no longer works after the scene switch while other UI elements such as buttons still as like no problem. I try to clear all the listeners in On value Change but still had no luck. And after switching scenes the only way I get it to work is by adding the method that I hope to trigger in On value change through the inspector. After I learnt that it works in that way, then I try to add it through script using Add listener. But still, it is not working.
    Also, I realise after I add listeners to On Value Change. Although the method is triggered, the inspector didn't update even with Refresh Value.
    Which is wried because, If I add the method to On value change through the inspector, it works, but if I add it through script using add listener it doesn't work(after switching scenes).
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Is it the same dropdown? Or a different one in the new scene?

    And if in the first scene it is connected to things that go away in the second scene (or are replaced), then you need to reconnect the new scene elements.

    A GameManager or UIManager construct to manage this noise is usually the pattern.
     
  3. hiaron123

    hiaron123

    Joined:
    Jul 18, 2019
    Posts:
    28
    The wried part is, when I selected the drop-down after I switch scene, everything like the script that I tried to call is still on to it. However, it just doesn't work, not until I manually add the method to On Value change through the inspector. I have created another button to add listeners to the On value change through the script. But it doesn't work after switching the scene worth mentioning another button still works so I can confirm I did trigger it. But it only works on the first one. Which is wried because it only works when I change it through the inspector.(after switch scene)

    And since I am just loading back to the same scene like a restart. If it works the first time, it should work the second time as well unless something gets destroyed. but it shouldn't since I didn't use don't destroy on load. everything should reset and I give the reference by using drag and drop. So it should be ok, I have no clue now.
     
  4. hiaron123

    hiaron123

    Joined:
    Jul 18, 2019
    Posts:
    28
    Yes, it is the same drop-down, well I mean maybe a new one? since I am loading to the same scene like a restart and I didn't use any don't destroy on load. Everything should be reset right? Everything else works fine except the drop-down.
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    I'll guess it is a faulty "singleton" or "highlander" implementation, one of those "if it already exists then destroy myself" type constructs, which are notoriously mis-implemented.

    Here is some timing diagram help:

    https://docs.unity3d.com/Manual/ExecutionOrder.html

    ULTRA-simple static solution to a GameManager:

    https://forum.unity.com/threads/i-need-to-save-the-score-when-the-scene-resets.1168766/#post-7488068

    https://gist.github.com/kurtdekker/50faa0d78cd978375b2fe465d55b282b

    OR for a more-complex "lives as a MonoBehaviour" solution...

    Simple Singleton (UnitySingleton):

    Some super-simple Singleton examples to take and modify:

    Simple Unity3D Singleton (no predefined data):

    https://gist.github.com/kurtdekker/775bb97614047072f7004d6fb9ccce30

    Unity3D Singleton with a Prefab (or a ScriptableObject) used for predefined data:

    https://gist.github.com/kurtdekker/2f07be6f6a844cf82110fc42a774a625

    These are pure-code solutions, do not put anything into any scene, just access it via .Instance!

    If it is a GameManager, when the game is over, make a function in that singleton that Destroys itself so the next time you access it you get a fresh one, something like:

    Code (csharp):
    1. public void DestroyThyself()
    2. {
    3.    Destroy(gameObject);
    4.    Instance = null;    // because destroy doesn't happen until end of frame
    5. }
    There are also lots of Youtube tutorials on the concepts involved in making a suitable GameManager, which obviously depends a lot on what your game might need.
     
  6. hiaron123

    hiaron123

    Joined:
    Jul 18, 2019
    Posts:
    28
    Thanks for the reply, I found the problem, I set the function if a static bool is true which will return. I didn't know to reload the scene doesn't reset the static value. Thanks for the help