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

False Unreachable Code Detected warningS

Discussion in 'Scripting' started by Flynn_Prime, Aug 29, 2018.

  1. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    383
    The code below shows the warning, with the "i" in "i++" underlined in Visual Studio. The code functions exactly as it should do, so I am not sure why I am getting this error. Is there anyway to fix this? If not, can I suppress the warning?

    Code (CSharp):
    1.     public static List<Ability> LoadExpertiseAbilityList(AbilityExpertise type)
    2.     {
    3.         List<Ability> temp = new List<Ability>();
    4.  
    5.         for (int i = 0; i < abilityDatabase.Count; i++)
    6.         {
    7.             Ability a = abilityDatabase[i];
    8.             if (a.expertise == type)
    9.                 temp.Add(a);
    10.             return temp;
    11.         }
    12.         return null;
    13.     }
     
  2. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Are you sure this code is working as you intend? (Hint, Visual Studio is correct in the warning it is giving you :)).

    Try stepping through this code in the debugger. Do you notice anything about what happens?
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Have you tried any test cases where more than 1 ability gets returned?
     
    Doug_B likes this.
  4. Fido789

    Fido789

    Joined:
    Feb 26, 2013
    Posts:
    343
    return temp;
    will quit the method (and for loop) right on the first iteration,
    i++
    will never be executed.

    Btw, I strongly suggest you to use some more descriptive names for your variables.
     
    Last edited: Aug 29, 2018
    CodeSmile likes this.
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    3,915
    For a utility method the ‚a‘ is fine. The temp I don‘t like, as it could be anything. I would use ‚matchingAbilities‘.
     
  6. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    383
    Doh.... I need to stop making these silly mistakes...

    Code (CSharp):
    1.     public static List<Ability> LoadExpertiseAbilityList(AbilityExpertise type)
    2.     {
    3.         List<Ability> matchedAbilities = new List<Ability>();
    4.  
    5.         for (int i = 0; i < abilityDatabase.Count; i++)
    6.         {
    7.             Ability a = abilityDatabase[i];
    8.             if (a.expertise == type)
    9.             {
    10.                 matchedAbilities.Add(a);
    11.             }
    12.         }
    13.         return matchedAbilities;
    14.     }
    Hoping this is now correct? Not got unity open right now to test...
     
  7. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    Hi ItzChris92,
    Out of interest, would you have got to the answer from just the first 2 responses on this thread alone?

    I think that it will be valuable for you to become comfortable with debugging code through when confronted with issues where, for example, code is not acting as you expect. :)
     
  8. Flynn_Prime

    Flynn_Prime

    Joined:
    Apr 26, 2017
    Posts:
    383
    I figured it out after your first comment... even though I had gone through the code line by line with the issue staring me in the face and I didn't see it :/

    I debug my code a lot, but I hadn't thought to try to add more abilities to my database (I only have one currently, just setting the base for everything) as I thought the method was working correctly because it wasn't returning null.

    Appreciate the help guys. Stay tuned for more ridiculous overlooked mistakes on my part :D
     
    Doug_B likes this.