not all code paths return a value

I have this code for recognize to tablet or mobile but giving error
the code is this:

    public static ENUM_Device_Type GetDeviceType()
    {
#if UNITY_IOS
        bool deviceIsIpad = UnityEngine.iOS.Device.generation.ToString().Contains("iPad");
        //bool deviceIsIphone = UnityEngine.iOS.Device.generation.ToString().Contains("iPhone");
        if (deviceIsIpad)
            {
                return ENUM_Device_Type.Tablet;
            }
           
            else
            {
                return ENUM_Device_Type.Phone;
            }
#elif UNITY_ANDROID
        float aspectRatio = Mathf.Max(Screen.width, Screen.height) / Mathf.Min(Screen.width, Screen.height);
        bool isTablet = (DeviceDiagonalSizeInInches() > 6.5f && aspectRatio < 2f);
        if (isTablet)
        {
            return ENUM_Device_Type.Tablet;
        }
        else
        {
            return ENUM_Device_Type.Phone;
        }
#endif
    }

I think because of #if, but it has to be like this. anybody help?

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.

1 Like

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.

2 Likes

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.

    public float Oops
    {
        get
        {
            #if FOO
            return 3;
            #endif
        }
    }

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 :stuck_out_tongue:

The preprocessor runs before the compiler gets to look at the code. As far as it’s concerned, that’s a completely empty function!

1 Like

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.

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:

public static ENUM_Device_Type GetDeviceType()
{

}

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.

1 Like

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.

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.

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.

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

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.