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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Not executing, even though conditional is true

Discussion in 'Scripting' started by Tarball, Jan 9, 2018.

  1. Tarball

    Tarball

    Joined:
    Oct 16, 2016
    Posts:
    166
    I'm losing my mind here. I'm sure this is simple, but I can't seem to figure out why it won't work. First, the following function:

    Code (CSharp):
    1.  
    2.     private void blocking_function()
    3.     {
    4.         if (Input.GetButton("block_button"))
    5.         {
    6.             blocking = true;
    7.             player_animator.SetTrigger("block");
    8.         }
    9.         else blocking = false;
    10.     }
    11.  
    The animation plays, and I see "blocking" stay checked as true in the inspector while "block_button" is held. However, the following code will not execute.

    Code (CSharp):
    1. if (blocking)
    2. {
    3. /// slow down
    4. }
    If I change "blocking" to "Input.GetButton(KeyCode.Y)", it executes fine with the Y key held. If "blocking" is true, why won't
    Code (CSharp):
    1. if(blocking)
    evaluate?
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,383
    Because blocking gets modified elsewhere?

    Post the whole script.
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Did you debug blocking right before the if check to see what value it is?
     
  4. Tarball

    Tarball

    Joined:
    Oct 16, 2016
    Posts:
    166
    The thing is that there are numerous scripts involved. Here is the script with the "blocking function()"

    Code (CSharp):
    1. public class Attack_Manager : MonoBehaviour
    2. {
    3.     private Switch_Weapons switch_script;
    4.     private Player_Control player_script;
    5.     private float combo_timer1;
    6.     private float combo_timer2;
    7.  
    8.     private void attack_algo()
    9.     {
    10.         if (!switch_script.switching && !player_script.rolling && !player_script.swimming && !player_script.attacking)
    11.         {
    12.             if (switch_script.equipped[1])
    13.             {
    14.                 bow_attack();
    15.             }
    16.             else combo_attack();
    17.  
    18.         }
    19.     }
    20.  
    21.     private void combo_attack()
    22.     {
    23.         if (!player_script.blocking)
    24.         {
    25.             if (Input.GetButtonDown("Fire1") && Time.time - combo_timer1 > 0.9f && Time.time - combo_timer2 > 0.9f)
    26.             {
    27.                 player_script.attacking = true;
    28.                 combo_timer1 = Time.time;
    29.                 switch_script.player_animator.SetTrigger("light_attack");
    30.                 Invoke("Attack_Ready", 0.6f);
    31.             }
    32.             else if (Input.GetButtonDown("Fire1") && Time.time - combo_timer1 < 0.9f)
    33.             {
    34.                 player_script.attacking = true;
    35.                 combo_timer1 += 0.3f;
    36.                 combo_timer2 = Time.time;
    37.                 switch_script.player_animator.SetTrigger("medium_attack");
    38.                 Invoke("Attack_Ready", 0.6f);
    39.             }
    40.             else if (Input.GetButtonDown("Fire1") && Time.time - combo_timer2 < 1f)
    41.             {
    42.                 combo_timer2 += 0.4f;
    43.                 player_script.attacking = true;
    44.                 switch_script.player_animator.SetTrigger("heavy_attack");
    45.                 Invoke("Attack_Ready", 0.6f);
    46.             }
    47.         }
    48.         blocking_function();
    49.        
    50.     }
    51.  
    52.     private void Attack_Ready()
    53.     {
    54.         player_script.attacking = false;
    55.     }
    56.  
    57.     private void blocking_function()
    58.     {
    59.         if (Input.GetButton("block_button"))
    60.         {
    61.             player_script.blocking = true;
    62.             switch_script.player_animator.SetTrigger("block");
    63.         }
    64.         else player_script.blocking = false;
    65.     }
    66.  
    67.     private void bow_attack()
    68.     {
    69.  
    70.     }
    71.  
    72.     private void Start()
    73.     {
    74.         switch_script = gameObject.GetComponent<Switch_Weapons>();
    75.         player_script = gameObject.GetComponent<Player_Control>();
    76.         combo_timer1 = Time.time + 1f;
    77.         combo_timer2 = Time.time + 1f;
    78.     }
    79.  
    80.     private void Update()
    81.     {
    82.         attack_algo();
    83.     }
    84. }
    Now, "Switch_Weapons" has no "blocking" or "blocking_function" anywhere. However, blocking is a bool in the class "Player_Control" in a separate script. This one is too long to post, but the gist of it is:

    Code (CSharp):
    1.     private void Movement_Function()
    2.     {
    3.         if (!falling && !jumping && !rolling && !swimming)
    4.         {
    5.             velocity_unit_vector = cam.transform.TransformDirection(new Vector3(Input.GetAxisRaw("Horizontal"), 0f, Input.GetAxisRaw("Vertical")));
    6.  
    7.             if (blocking) // || Input.GetButton("Strafe_Shift"))
    8.             {
    9.                 strafe_forward = Input.GetAxisRaw("Horizontal") / 2f;
    10.                 strafe_side = Input.GetAxisRaw("Vertical") / 2f;
    11.                 switch_script.player_animator.SetFloat("Velocity X", strafe_forward);
    12.                 switch_script.player_animator.SetFloat("Velocity Z", strafe_side);
    13.             }
    14.             else
    15.             {
    16.                 forward = Input.GetAxisRaw("Horizontal");
    17.                 side = Input.GetAxisRaw("Vertical");
    18.                 switch_script.player_animator.SetFloat("Velocity X", forward);
    19.                 switch_script.player_animator.SetFloat("Velocity Z", side);
    20.             }
    21.         }
    22.     }
    Then, there is
    Code (CSharp):
    1.     private void Update()
    2.     {
    3.         Movement_Function();
    4.     }
    Any help spotting the problem would be greatly appreciated.
     
  5. Tarball

    Tarball

    Joined:
    Oct 16, 2016
    Posts:
    166
    I don't know how to do that. I'll look into it. Thanks.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Code (csharp):
    1. print("Value of blocking is: " + blocking);
    2. // aka Debug.Log( /* the same */);
    :)
     
  7. Tarball

    Tarball

    Joined:
    Oct 16, 2016
    Posts:
    166
    Ooooh, lol... OK, I did that right before the check, and it prints nothing but true as long as block is held. Still, the if block won't execute though. The thing is, as I said before, if I change "blocking" to input.whatever, it works fine when input is pressed.
     
    Last edited: Jan 9, 2018
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Did you put another print inside the if to make sure it wasn't also running?
     
  9. Tarball

    Tarball

    Joined:
    Oct 16, 2016
    Posts:
    166
    Wow, OK, I feel dumb now -- it is running. I assumed it wasn't, because the code wasn't executing as intended. The thing is that it works with input.whatever. So, something else must be the problem.

    animator.setFloat gets set for input.whatever, but not for the bool blocking. ??? They are both bools. I'm sorry if the question/title was wrong, but I'm still out of luck here.
     
    Last edited: Jan 9, 2018
  10. Tarball

    Tarball

    Joined:
    Oct 16, 2016
    Posts:
    166
    OK, I found the problem... Thanks for the idea of debug. That helped me find it out. I forgot I had that function controlling animation -- not the actual motion. Motion handled not by root animation but a different function entirely. Case closed. Thanks @Brathnann and everyone else. It turns out I forgot how my own code worked... I guess I need comments.
     
  11. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    No problem. Glad you got it working. Debug.Log and print are valuable tools and are really simple to just insert into code. :)