Change color with Custom Trigger

I’m trying to change the color of the material with the Distance Check Behavior script
Logger is clear

// -----JS CODE-----
// @input Asset.Material material
// @input vec4 color = {1.0,1.0,1.0,1.0}

function onTrigger() {
    print("on trigger");

    // Remove our onTrigger() function from the custom trigger's responses
    global.behaviorSystem.removeCustomTriggerResponse("my_trigger", onTrigger);
        
var originalColor = script.api.material.mainPass.baseColor;
 // Change it's baseColor
var NewColor = new vec4(Math.random(), Math.random(), Math.random(), 1.0);
    
    script.api.material.mainPass.baseColor = NewColor;

}

global.behaviorSystem.addCustomTriggerResponse("my_trigger", onTrigger);

Hmm, I’m not sure how the distance check works, but it might require the objects to be in 3D space. You might be able to tell by digging into the behavior script.

Also, I’m pretty sure the function would throw an error because it should be accessing the parameters like this: script.material.mainPass, not script.api.material.mainPass

1 Like

Hmm… dunno, but the Distance checker works somehow, It has the ability ‘Flatten Z Distance’ I guess its mean don’t use 3d space.

Also, I’m pretty sure the function would throw an error because it should be accessing the parameters like this: script.material.mainPass , not script.api.material.mainPass

Im trying it both, but nothing happens.

Maybe its has another solution that u can pointing me out.

What happens if you call onTrigger() manually?

If you post the project files, or a reduced test case, I can play around with it and see what works

Ive try to toggle Animation by Distance Check
and also change the color with it

Here is project

I think you have an extra distance check script. In any case, I edited this one to run “On Awake”

Also changed the distance check to use “Is less than” instead of “equals exactly”. Lastly, I unchecked “Allow repeat” to prevent the trigger from firing multiple times.

global.behaviorSystem.removeCustomTriggerResponse("my_trigger", onTrigger);

I’m not sure why, but this line doesn’t seem to do anything, so you can remove it. Also, I don’t think you want to unbind the event.

1 Like