I'm doing an eye color change using the NativeUI scripting (because picker options exceed 10). But I keep getting the error:
Cannot set property 'material' of null
script.js:103:6
Here's the script:
// Selected Value - NativeUI Picker example v85
// by Luke Hurd :: @lukehurd
// WHAT HAS CHANGED FROM PREVIOUS VERSIONS//
// In order to load Textures, Materials, and Objects, we must
// now use something in Javascript called "Promises". The basic
// concept is Spark AR now wants to make sure these assets are
// available to the script to manipulate before executing any code.
// When loading assets, find() has been changed to findFirst() and findAll()
// Load the modules
const Scene = require('Scene');
const Materials = require('Materials');
const NativeUI = require('NativeUI');
const Textures = require('Textures');
// First, we create a Promise and load all the assets we need for our scene
// The following example shows how to load Textures, Materials, and an Object.
Promise.all([
// Loading Textures for the buttons
// I will be using 12 icons for 12 Materials
Textures.findFirst('icon01'),
Textures.findFirst('icon02'),
Textures.findFirst('icon03'),
// Loading the Materials we are switching on the plane
// I'm only currently using three Mats and Icons for testing
Materials.findFirst('Amethyst_Mat'),
Materials.findFirst('Blue_Mat'),
Materials.findFirst('BrilliantBlue_Mat'),
// Loading the plane
Scene.root.findFirst('plane0'),
// Now, we wait for a "go ahead" from the script to let us know when
// we can start using our assets and creating the NativeUI Picker
]).then(function(results){
// Assets are loaded, so let's set them all so we can use them later in the script.
// The assets are all returned in an object called "results" in the order that we
// loaded them. Remember, the index starts at 0 so the first asset is always results[0],
// the next is results[1], etc.
// First, we set the buttons for the NativeUI Picker
const icon01 = results[0];
const icon02 = results[1];
const icon03 = results[2];
// Next, we set the materials for the plane
const Amethyst = results[3];
const Blue = results[4];
const BrilliantBlue = results[5];
// Finally, we set the plane
const plane = results[6];
// Now, we can finally start building the NativeUI Picker
const configuration = {
// This index controls where the NativeUI Picker starts.
// In this example, we want to start on the second button,
// not the first. So we pass the initial value of "1" (which is the second button)
selectedIndex: 0,
// These are the image textures to use as the buttons in the NativeUI Picker
items: [
{image_texture: icon01},
{image_texture: icon02},
{image_texture: icon03}
],
// These are the materials we are switching between on the plane
mats: [
{material: Amethyst},
{material: Blue},
{material: BrilliantBlue}
]
};
// Create the NativeUI Picker
const picker = NativeUI.picker;
// Load our configuration
picker.configure(configuration);
// Show the NativeUI Picker
picker.visible = true;
picker.selectedIndex.monitor({fireOnInitialValue: true}).subscribe(function(val) {
plane.material = configuration.mats[val.newValue].material
});
});
I will need to load/use 12 picker options for 12 materials.
I'm not really a programmer, so I'm just copy pasting the scripts I find on google. This script loads all my picker options (12 icons) with no issue. But they won't load their corresponding MATERIAL. In the patch editor, I've added the script producer patch and then used 'equals exactly' patch to load up the mats, but they don't work.