0
0
Unityframework~20 mins

Platform-specific settings in Unity - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Platform Pro in Unity
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
What is the output of this platform check code?
Consider this Unity C# script snippet that checks the platform using preprocessor directives and prints a message. What will be printed if the game runs on an Android device?
Unity
using UnityEngine;

public class PlatformCheck : MonoBehaviour
{
    void Start()
    {
        #if UNITY_ANDROID
            Debug.Log("Running on Android");
        #elif UNITY_IOS
            Debug.Log("Running on iOS");
        #else
            Debug.Log("Running on another platform");
        #endif
    }
}
ARunning on iOS
BRunning on Android
CRunning on another platform
DNo output, code will not compile
Attempts:
2 left
💡 Hint
Look at the preprocessor directives and which one matches Android.
Predict Output
intermediate
1:30remaining
What does this code print on Windows standalone build?
Given this Unity C# code snippet, what will be printed when running on a Windows standalone build?
Unity
using UnityEngine;

public class PlatformMessage : MonoBehaviour
{
    void Start()
    {
        if (Application.platform == RuntimePlatform.WindowsPlayer)
        {
            Debug.Log("Windows Standalone");
        }
        else
        {
            Debug.Log("Other Platform");
        }
    }
}
AWindows Standalone
BOther Platform
CNo output
DRuntime error
Attempts:
2 left
💡 Hint
Check the Application.platform property and the RuntimePlatform enum.
🔧 Debug
advanced
2:00remaining
Why does this platform-specific code cause a compile error?
This Unity C# script uses platform-specific code but causes a compile error. Identify the cause.
Unity
using UnityEngine;

public class PlatformBug : MonoBehaviour
{
    void Start()
    {
        #if UNITY_IOS
            Debug.Log("iOS platform");
        #elif UNITY_ANDROID
            Debug.Log("Android platform");
        #else
            Debug.Log("Other platform");
        #endif

        #if UNITY_EDITOR
            Debug.Log("Editor mode");
        #endif

        #if UNITY_WIN
            Debug.Log("Standalone build");
        #endif
    }
}
AUsing multiple #if directives in one method causes syntax errors.
BThe code is correct and compiles without errors.
CUNITY_EDITOR cannot be used with other platform directives in the same method.
DUNITY_WIN is not a valid preprocessor directive, causing a compile error.
Attempts:
2 left
💡 Hint
Check Unity's official list of valid platform defines.
📝 Syntax
advanced
2:00remaining
Which option correctly uses platform-specific code to include a method only on iOS?
You want to include a method called ShowIosAlert() only when building for iOS. Which code snippet correctly uses preprocessor directives?
A
#if UNITY_IOS
public void ShowIosAlert() {
    Debug.Log("iOS Alert");
}
#endif
B
public void ShowIosAlert() {
#if UNITY_IOS
    Debug.Log("iOS Alert");
#endif
}
C
public void ShowIosAlert() {
#if UNITY_IOS
    Debug.Log("iOS Alert");
#else
    Debug.Log("Not iOS");
#endif
}
D
public void ShowIosAlert() {
#if UNITY_IPHONE
    Debug.Log("iOS Alert");
#endif
}
Attempts:
2 left
💡 Hint
Preprocessor directives can wrap entire methods to exclude them from compilation.
🚀 Application
expert
3:00remaining
How to implement platform-specific file path handling in Unity?
You want to save a file in a platform-appropriate location in Unity. Which approach correctly handles platform-specific file paths?
AUse hardcoded absolute paths like "C:/MyGame/" for Windows and "/Documents/MyGame/" for iOS inside #if directives.
BUse Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) for all platforms without Unity-specific paths.
CUse Application.persistentDataPath for all platforms and append platform-specific subfolders using #if directives.
DUse Application.dataPath for saving files on all platforms without any platform checks.
Attempts:
2 left
💡 Hint
Unity provides special paths that adapt to each platform's file system.