Material change on tap

  • Hi Joshua,

    Here's a script that will cycle through materials, applying them to a MeshVisual you specify. Add it to the "Initialized" event on a ScriptComponent, then hook up your MeshVisual component and Material assets.

    // Use in "Initialize" event
    // @input Component.MeshVisual meshVisual
    // @input Asset.Material[] materials

    // Our starting texture index
    var currentItemIndex = 0;

    // Function to clear the current materials and add a new one
    function setMaterial(material){

    // Assign the initial material to our meshVisual

    // Define what happens when you tap.
    function changeMaterial () {

    // Increment the current item index
    currentItemIndex += 1;

    // We need the current item index to wrap around
    // once it's higher than the number of items we have.
    currentItemIndex = currentItemIndex % script.materials.length;

    // Change the mesh's material

    // Bind the function to the tap event.
    var touchEvent = script.createEvent("TapEvent");

    You can also check out this guide for more scripts and tips on cycling through items on tap. I adapted one of the scripts in there for the script above. You can also modify the script to use vec4 instead of Materials, if you'd prefer to use a single material instead and cycle through colors on it.

    Let me know if you need any more help!


    Comment actions Permalink
  • Thank you so much! This was exactly what I needed :)

    Comment actions Permalink
  • I get this every time I complete my cycle of my two different items:


    11:04:56 TypeError: cannot write property 'enabled' of undefined
    at activateNextItem (Change on tap-Script:20) preventsyield


    When I push lens to device it works but then it reach the end of the cycle it says lens error.

    Comment actions Permalink
  • Hey Martin,

    I think you may actually be using the script from this post? If so, did you modify the script at all? It looks like line 20 is blank in the original version.

    If it's the unchanged script, I'm not sure what could be causing that, since it's working for me with two objects. Is it possible one of the objects is getting destroyed? If not can you post the full script you're using?


    Comment actions Permalink
  • Is there a way you can clear the other materials from the scene entirely so that it doesn't impact the FPS or Ram usage? When looking at the FPS and ram, they both go up every time I cycle to a new material until I've cycled through them all. Could there be a way that only one material impacts the scene?

    Comment actions Permalink
Have a comment?

Please sign in to leave a comment.

We're here to help! We just need a little bit of information...

What system are you using?

Have you downloaded the latest version of Lens Studio?

Please download the latest version of Lens Studio. If you still run into this issue, please come back and report it!

Is this issue causing Lens Studio to crash?

What system do you run Lens Studio on?



Follow the next steps to help us solve your issue:

  • Copy and paste this text into your TerminalCommand Window
    open ~/Library/Preferences/Snap/Lens\ Studio/ %LOCALAPPDATA%\Snap\Lens Studio Copy Text
  • Press ReturnEnter to run the command. The Lens Studio folder will automatically open
  • Prepare to upload your files: zip the "Log" Folder by right-clicking and choosing "compress."
    Locate the Log.txt file right above it.

    Attach a screenshot of the issue:



What is this most relevant to?

Please enter a brief description of your issue:

Thanks for submitting this issue.

Unfortunately, it's likely due to the operating system or hardware you're using – since they don't meet the system requirements for Lens Studio.

Still, we hear you loud and clear, and are logging the issue in case there's ever a workaround we can provide!

Minimum Requirements

Operating System: Windows 10 (64 bit); MacOS 10.11+

Hardware: Minimum of Intel Core i3 2.5Ghz or AMD Phenom II 2.6Ghz with 4 GB RAM; Intel HD Graphics 4000 / Nvidia GeForce 710 / AMD Radeon HD 6450 or better; screen resolution of 1280x768 or higher

We'll try to resolve this issue as soon as possible. Thanks for letting us know about it!

Keep an eye out for a followup email from us. We may have a couple more questions for you, or we might already have a solution to offer.

Happy creating!