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

Question Return statement isn't called inside an IF statement

Discussion in 'Getting Started' started by coldgod04, Jan 11, 2023.

  1. coldgod04

    coldgod04

    Joined:
    Jul 16, 2021
    Posts:
    2
    Hi guys,
    I'm working on a state machine for my player controller and having some trouble.
    So, I have a state named "PlayerAbilityState", it has a bool named "isAbilityDone". The state will change if "isAbilityDone" is true and before changing the state, "isAbilityDone" is changed back to false.
    The problem is that the return statement for changing the state is never called after "isAbilityDone" being changed back to false. Please help me understand my mistake here!

    I'm sorry if my question is too difficult to understand (I'm not the best at English)
    Thank you!

    Code (CSharp):
    1. public class PlayerAbilityState : PlayerNotUsingUIState
    2. {
    3.     public static bool isAbilityDone = false;
    4.  
    5.     public override PlayerBaseState DoState(PlayerStateMachine playerBehavior)
    6.     {
    7.         if (base.DoState(playerBehavior) != playerBehavior.notUsingUIState) return base.DoState(playerBehavior);
    8.  
    9.         if(isAbilityDone == true)
    10.         {
    11.             isAbilityDone = false;
    12.             if (playerBehavior.playerData.IsGrounded() == true)
    13.             {
    14.                 return playerBehavior.onGroundState;    //This line is never called
    15.             }
    16.             if (playerBehavior.playerData.IsGrounded() == false)
    17.             {
    18.                 return playerBehavior.onAirState;       //This line is never called
    19.             }
    20.         }
    21.         return playerBehavior.abilityState;
    22.     }
    23. }
     
  2. coldgod04

    coldgod04

    Joined:
    Jul 16, 2021
    Posts:
    2
    I figured it out...
    I'm so dumb that I call the "base.DoState()" function twice in the first IF statement. That the thing messing up the code.
    But thank you anyway! <3
     
  3. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    Don't worry about it, that's how you learn, by doing it wrong, and then trying to figure out WHY you did it wrong, and then do it right.