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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Question not all code paths return a value

Discussion in 'Scripting' started by freedom667, Jan 18, 2023.

  1. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    I have this code for recognize to tablet or mobile but giving error
    the code is this:
    Code (CSharp):
    1.     public static ENUM_Device_Type GetDeviceType()
    2.     {
    3. #if UNITY_IOS
    4.         bool deviceIsIpad = UnityEngine.iOS.Device.generation.ToString().Contains("iPad");
    5.         //bool deviceIsIphone = UnityEngine.iOS.Device.generation.ToString().Contains("iPhone");
    6.         if (deviceIsIpad)
    7.             {
    8.                 return ENUM_Device_Type.Tablet;
    9.             }
    10.            
    11.             else
    12.             {
    13.                 return ENUM_Device_Type.Phone;
    14.             }
    15. #elif UNITY_ANDROID
    16.         float aspectRatio = Mathf.Max(Screen.width, Screen.height) / Mathf.Min(Screen.width, Screen.height);
    17.         bool isTablet = (DeviceDiagonalSizeInInches() > 6.5f && aspectRatio < 2f);
    18.         if (isTablet)
    19.         {
    20.             return ENUM_Device_Type.Tablet;
    21.         }
    22.         else
    23.         {
    24.             return ENUM_Device_Type.Phone;
    25.         }
    26. #endif
    27.     }
    I think because of #if, but it has to be like this. anybody help?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    What if neither of those #if targets is true?

    Throw a final return at the end to shut it up, and put it in an #else if you want to keep the warnings away.
     
    Bunny83 likes this.
  3. Juice-Tin

    Juice-Tin

    Joined:
    Jul 22, 2012
    Posts:
    233
    What a out when testing on PC?

    there always needs to be an edge case where none of the if’s are met. The compiler won’t go through because there’s a chance it may not be a mobile device and thus will have nothing to return.

    just add an #else as the end there before your #endif and return some kind of null or random value. Even if you know it won’t trigger, it will make the compiler happy.
     
    Bunny83 and Kurt-Dekker like this.
  4. chemicalcrux

    chemicalcrux

    Joined:
    Mar 16, 2017
    Posts:
    717
    To be pedantic, the compiler doesn't try to reason about whether or not the #if and #elif macros could resolve in a way that results in bogus code. You'll only stumble on the problem if it's in such a configuration.

    Code (CSharp):
    1.     public float Oops
    2.     {
    3.         get
    4.         {
    5.             #if FOO
    6.             return 3;
    7.             #endif
    8.         }
    9.     }
    If FOO is defined, then this raises no errors or warnings. If it isn't defined, the compiler complains.

    So, in this case, you must have your project configured so that neither UNITY_IOS nor UNITY_ANDROID are defined.

    The compiler doesn't say "hey, if UNITY_IOS isn't defined, and UNITY_ANDROID isn't defined, then this will be broken!". It would be very annoying if it tried to think about code that's being excluded by preprocessor directives :p

    The preprocessor runs before the compiler gets to look at the code. As far as it's concerned, that's a completely empty function!
     
    Bunny83 likes this.
  5. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    codes are true. it just giving "not all code paths return a value". Obviously because of them it doesn't see the "return" code in it.
     
  6. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,938
    It's doing so because there's some potential at compile time for none of the conditional segments to be compiled, and thus you end up with an empty method that returns nothing (ergo invalid code), like this:
    Code (CSharp):
    1. public static ENUM_Device_Type GetDeviceType()
    2. {
    3.  
    4. }
    Obviously to the compiler that ain't Gucci. So just do what Kurt says and throw an
    #else
    in there.

    The compiler shall not be argued with.
     
    Bunny83 likes this.
  7. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,541
    Well, not really. The error is only thrown by the compiler when the method is actually empty. So neither UNITY_IOS nor UNITY_ANDROID is set. The compiler does not even look at what's inside a section that is not active. Such a section could contain a bazillion compiler errors. However as long as it's not active, the compiler does not care about it at all. So when he actually gets this error, it means that both defines are not defined. That can only happen when the target platform in Unity is set to something different (not IOS and not Android), like Standalone, WebGL, whatever.
     
  8. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,541
    Are you 100% sure that the code you posted is your actual code and that your platform is set to either IOS or Android in Unity? if that's the case, you can't get the error you mentioned. Since you seem to have copied the code from here, is it possible that you haven't changed it? Because the code over there would cause that error when you're on IOS because the code over there is wrong. It has two seperate if statements inside the IOS section and no general return. So the IOS section would throw that error, the Android section would not.

    If that's really your issue, you just wasted everyone's time here by not posting your actual code.
     
  9. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,938
    Ah, I had the wrong idea. I figured Visual Studio just gives a visual (hah!) indication as to which defines are active or not... which I guess it still is, but also indicating the compiler is completely ignoring said code. Makes more sense now.
     
  10. freedom667

    freedom667

    Joined:
    Sep 6, 2015
    Posts:
    425
    It's not my fault because this code is works before. why compiler giving error because I refreshed the project. then compiler started give this error. this is not my code I took from another page and everybody says "it works" and me too. but after refreshing the project, giving error
     
  11. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,541
    Yes, the code on that other page should work, but ONLY if your target platform in Unity is set to Android. It would fail on any other platform. YOUR code, which is slightly different from the one I linked above is different as it has at least fixed the bug in the iOS section. So the code YOU posted here should work only on Android or iOS but would still fail on any other target platform. The code is not designed to work on any other platform. How to make the code that YOU posted work was already explained by the second and third response in this thread here. The original code that I linked above that you probably copied, does not work when the target is iOS because that code has an error in it. That code over there only works on Android.

    I don't know how to be any more precise about your code and the original code and the possible reasons for the error you see.

    So
    • either the code you use that produces the error is not the code that you posted here.
    • or if the code you posted is your actual code, it would fail on any other target platform (not iOS and not Android)
    • or if the code you use is the original code, it should only work on Android and on no other target platform.