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

Toggle execution order.

Discussion in 'UGUI & TextMesh Pro' started by PSpiroz, Feb 10, 2017.

  1. PSpiroz

    PSpiroz

    Joined:
    Feb 15, 2015
    Posts:
    21
    Hi there,
    Does anybody know the order of Toggle methods execution.
    I am having a script that runs in editors "onValueChange()":
    Code (CSharp):
    1. public void ffwd()
    2. {
    3.     if (myToggle.isOn==true)
    4.     {
    5.             Debug.Log("FFWD");
    6.             audioPlay.pitch = 4;
    7.     }
    8.         else if(myToggle.isOn == false)
    9.     {
    10.             Debug.Log("FFWD back to normal");
    11.             audioPlay.pitch = 1;
    12.     }
    13. }
    This is a method in a script attached to the parent of the toggle.
    What I am expecting is that by clicking 1st time the toggle (from off --> on):
    1st) IsOn to became true
    2nd) Since (toggle.isOn == true ) --> I expect to console to print "FFWD" and
    3rd) pitch to go at 4.

    And when I push toggle again,
    1st) IsOn to became false
    2nd) The Console to print "FFWD back to normal"
    3rd) pitch to become 1

    What I get is always the results for myToggle.isOn == false
    (pitch to 1, and FFWD back to normal), even though the "isOn" in the inspector is being ticked. The only exception I got and get my method to work is momentary when I click the toggle very fast on and off.

    What I am thinking is that maybe Unity first executes code and then turns toggle to true.
    Could this be true, or I am missing something...?
     
  2. Hosnkobf

    Hosnkobf

    Joined:
    Aug 23, 2016
    Posts:
    1,078
    Which Unity Version are you working with?

    "onValueChange" is AFAIK Unity 5.2 and below. The wording has been revised to "onValueChanged". I am not sure about the old behavior but now "onValueChanged" is called after isOn has changed.

    However, you should be able to add a bool parameter in the method's header which indicates which value is the new value.
     
  3. PSpiroz

    PSpiroz

    Joined:
    Feb 15, 2015
    Posts:
    21
    I have Unity 5.5.1f1 (64), and indeed it is "on value Changed", I had never noticed that.
    What you propose is to make my manual change toggle, every time I press the button. My problem is that I change a toggles value from other methods too, and I got conflicts. Not things that can easily be corrected through script.
    I just had to ask because it has no explanation, and I don't need to write extra code.
    Does it been affected from time.scale ==0 ?