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

Second IF statement won't run?

Discussion in 'Scripting' started by Corva-Nocta, Apr 18, 2018.

  1. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    I am not sure why my code is not running the second IF statement in the list if the first is not true. I have a variable that is an enum that I am trying to check. If it is one value, then run the first IF statement, if its the next value then run the second. The first one works perfectly but when I change the enum variable to the second value it never gets to the second IF statement. Why is this? How do I fix it?

    Code (csharp):
    1.  
    2.  public void AddAttacks(Item item)
    3.     {
    4.         if (item.itemType == Item.ItemType.MainHand)
    5.         {
    6.             if (item.attack1 != null)
    7.             {
    8.                 MainHandAttacks.Add(item.attack1);
    9.                 GameObject attack1 = Instantiate(attackSlot, mainHandPanel.transform);
    10.                 attack1.GetComponentInChildren<Text>().text = item.attack1.name;
    11.                 attack1.GetComponent<AttackSlot>().attack = item.attack1;
    12.  
    13.                 attack1.GetComponent<AttackSlot>().SelectAttack();
    14.              }
    15.         }
    16.  
    17.         if (item.itemType == Item.ItemType.OffHand)
    18.         {
    19.             if (item.attack1 != null)
    20.             {
    21.                 OffHandAttacks.Add(item.attack1);
    22.                 GameObject attack1 = Instantiate(attackSlot, offHandPanel.transform);
    23.                 attack1.GetComponentInChildren<Text>().text = item.attack1.name;
    24.                 attack1.GetComponent<AttackSlot>().attack = item.attack1;
    25.             }
    26.       }
    27. }
    from what I can see, if the first IF statement is true then it should move down to the next one and check. I have also tried setting it to an ELSE IF statement and that doesn't change it at all. Any ideas?
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    As you have it now, both ifs will run a check, even if the first is true, the second if will check if it's an offhand as well.

    That being said, you should insert print/Debug.log statements in at points where you think it should be hitting and print out values to make sure you have the expected values.
     
  3. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    hm, it seems like if it is running both then it should be working. Not sure why it would be hanging up.

    I actually already inserted lots of debugs earlier, that's how I found out the second IF statement isn't running at all. Still no clue why
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    So when Item.ItemType was an OffHand, (your debug.log showed this) and you had additional debugs within the offhand if statements, they never showed up?
     
  5. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    actually if I set it to OffHand I never got any debug.logs, no matter where they were inside that IF statement. It just never runs and I have no idea why
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    How are you calling AddAttacks?
     
  7. Corva-Nocta

    Corva-Nocta

    Joined:
    Feb 7, 2013
    Posts:
    801
    And that was the problem. Where I was calling AddAttacks I was not calling it for the slot for the offhand, only for the main hand. Thanks for the help!
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    No problem. Glad you got it working!