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

[SOLVED] Unable to exit a function !!!

Discussion in 'Scripting' started by Agent003, Jan 12, 2020.

  1. Agent003

    Agent003

    Joined:
    Sep 7, 2018
    Posts:
    55
    Hello,
    I'm using a trigger to start this function (dynamically) meaning that the trigger send it's state to the function, i'm trying to exit that function before it reaches the second ELSE condition using return, but it always get to Else and execute it.

    Code (CSharp):
    1.     public void rlup(bool rltp) {
    2.         if (rltp) {
    3.             if (stcint [4] >= 60) {
    4.                 startFx (1);
    5.             } else {
    6.                 startFx (2);
    7.                 toggle [0].isOn = false;
    8.             }
    9.             return;
    10.         } else {
    11.             gameText [1].text = stcint [4].ToString ();
    12.             startFx (0);
    13.         }
    14.     }
     
  2. nchampion

    nchampion

    Joined:
    Jun 17, 2013
    Posts:
    2
    By the second else, do you mean the one under:
    Code (CSharp):
    1. else {
    2.             gameText [1].text = stcint [4].ToString ();
    3.             startFx (0);
    4.         }
    If so, that would be hit if rltp would be false. If rltp were true, it would never hit the clause, and the return is extraneous.
     
    Agent003 likes this.
  3. Agent003

    Agent003

    Joined:
    Sep 7, 2018
    Posts:
    55
    Yes, i mean the one you mentioned, it is executed even if the toggle is true (why: because if i have a coins less than 60 i set that toggle to false in line number 7), i want to exit the function after i set the toggle to false (after line 7), i don't want it to keep executing.
     
  4. Agent003

    Agent003

    Joined:
    Sep 7, 2018
    Posts:
    55
    The problem was that the toggle execute the function every time it's value change (not every click), so the function was get stopped by RETURN, but then the toggle switch execute the function again (because it was set to false in line number 7) and enter the second else condition. after one full day of searching, i found this thread which is full of different solutions to this problem.

    forum.unity.com/threads/change-the-value-of-a-toggle-without-triggering-onvaluechanged.275056/