Activate and deactivate object on mobile detection

Hi guys how can I activate a game object and deactivate another game object when it is detected that the browser is opened in mobile ?

I’m using this to detect mobile

import { isMobileDevice } from "@needle-tools/engine" if( isMobileDevice() )

Original Post on Discord

by user 546555823451668481

There’s a handy component called DeviceFlags for that :+1:

to detect mobile devide or to enable and disable gameobjects ?

by user 546555823451668481

You can add this to an object and it turns it on or off based on your selection from the device mask

So, this is to drive the objects directly without you writing any code :+1:

for visible off i should turn off the check box ?

by user 546555823451668481


so I’m doing this only for canvas

and I’m making a different canvas for mobile,

so i will just add this to desktop canvas and uncheck mobile? or should i uncheck for mobile for desktop canvas and for mobile canvas i should uncheck desktop ?

by user 546555823451668481

Most probably you want to drive both objects

okay, so will add on both the canvas

And will uncheck mobile and desktop respectively

and all other objects in the scene are activate on both mobile and desktop right ?

by user 546555823451668481

yes the rest of the scene will be untouched. If you are unsure about what a component does, just open it and have a look for yourself :slight_smile: Some are quite simple.

thanks it works perfectly

by user 546555823451668481

do we also have something like this for scripts that enable scripts by ckecking mobile/desktop ?

by user 546555823451668481

Not right now - but we could perhaps expose something like that in the DeviceFlags script via a UnityEvent :thinking: then you could do anything

script will be always on a gameobject, you can drive the gameobject with the DeviceFlags.

Do you feel you would want to enable/disable single script?

You can always do this in your components:

awake() {
   this.enabled = isMobileDevice();
   if(!this.enabled) return

   // rest of awake implem

isnt checking inside the script if the website is opened on mobile or not ?

by user 546555823451668481

Sorry, could you rephrase that? I’m not sure if i follow.

the above code you sent

isnt it checking if the website is opened on mobile or not ?

by user 546555823451668481


but how can i disable the script if i see the mobile device is detected?Sorry I’m not a Ts coder I’m using chatgpt so far.

I have two script to rotate the game object, one used mouse touch, other uses pointer (this one is for mobile)

If both scripts are enabled then the rotation on desktop is not affected but the rotation on mobile is affected and it only wants the pointer script on the object to rotated the game object smoothly, so on mouse rotation script i want that if the mobile device is detected the script disables itself

by user 546555823451668481