Search Unity

Why is this module executed when starting the game

Discussion in 'Scripting' started by pKallv, Aug 26, 2019.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    I have the following function:

    Code (CSharp):
    1. public void TGL_Replay(bool _tgl_Value)
    2.     {
    3.         print("TGL_Replay: " + _tgl_Value);
    4.         if (_tgl_Value)
    5.         {
    6.             PlayerPrefs.SetString("REPLAY", "TRUE");
    7.         }
    8.         else
    9.         {
    10.             PlayerPrefs.SetString("REPLAY", "FALSE");
    11.         }
    12.     }
    It is connected to a toggle and that is the only thing that execute it. Despite that it is executed when I start the game.

    Anyone having any ideas why this is happening?
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Toggles (and other inputs) generally call their attached functions when they are initialized. It's an annoying fact about the UI system, but you have to be ready to ignore anything that happens during startup on these calls if the default value screws up your logic..
     
    pKallv likes this.
  3. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,191
    As always I had done a mistake, mixed a SetActive with IsOn ...)/&€(&%#/&)€