Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I hide some objects on mobile platform builds

I have created the game for both PC and mobile devices like android, ios. I have added some joystic components on the screen to help mobile users navigate the player.

The problem is that the joystic also appears on PC version as well.

How do I hide such components from being displayed on non mobile platforms of the game.

like image 853
Vivek Avatar asked Feb 04 '26 15:02

Vivek


1 Answers

Detect if the platform is not a mobile device with Application.isMobilePlatform in the Awake or Start function then deactivate the Joystick GameObject. Make sure that the joystick UI stuff has a parent GameObject. That parent GameObject is what you should deactivate.

public GameObject joyStick;

void Awake()
{
    if (!Application.isMobilePlatform)
    {
        joyStick.SetActive(false);
    }
}

Again, disable GameObject, not the component. That should also disable the children objects and the scripts attached to them.

EDIT:

Thanks. I also want to the joystick script, gameobject to be excluded in non mobile platform builds. Does this also take care of it?

No. If you want it to be excluded in the scene then after creating a parent object for the joystick, make it a prefab and then delete it from the scene or the Hierarchy tab. You can then instantiate a prefab only if this is a mobile device.

public GameObject canvas;
public GameObject joyStickPrefab;

void Awake()
{
    if (Application.isMobilePlatform)
    {
        Instantiate(joyStickPrefab, canvas.transform, false);
    }
}

Note that the prefab will still be included in the build but will not be in the scene or loaded in memory unless it's a mobile device. There is really no way of excluding prefabs or assets from build. You can exclude scripts based on platform with Unity's preprocessor directives but you can't exclude assets which are the UI items from the jotystick.

like image 188
Programmer Avatar answered Feb 06 '26 04:02

Programmer