[Solved] What to do when a script input is undefined?

Loup_Bahroune
Loup_Bahroune Posts: 6
edited August 2023 in Programming #1

EDIT: It was an error from my side. I mispelled a variable name.
The solution that works is
if(global.backgroundTexture == undefined)


Hi everyone! :blush:

I'm looking to assign a script input (a texture) to a scene object (an image). I'm using 2 different scripts as this is a requirement for my project.
When the texture input is undefined, it's trying to apply it to my image, but as it's undefined, it makes Lens Studio crash.

In the first script, I have:
//@input Asset.Texture backgroundTexture

global.backgroundTexture = script.backgroundTexture;

In the second script, I have:
//@input Asset.Texture missingTexture
//@input Component.Image imageBackground

script.imageBackground.mainPass.baseTex = global.backgroundTexture;

I tried doing:
if(global.backgroundTexture == null) {
script.imageBackground.mainPass.baseTex = script.missingTexture;
}
else{
script.imageBackground.mainPass.baseTex = global.backgroundTexture;
}
But it's still crashing.

I also tried:
if(global.backgroundImage === undefined){
script.imageBackground.mainPass.baseTex = script.missingTexture;
}
else{
script.imageBackground.mainPass.baseTex = global.backgroundTexture;
}
And then it's not crashing, but when I update the texture, it still shows the missing texture.

Do you have any idea how to fix this?

Best regards :)

Answers

  • Bakari Mustafa
    Bakari Mustafa Posts: 174 🔥🔥🔥

    Try this script @Loup_Bahroune

    //@input Asset.Texture backgroundTexture
    
    global.backgroundTexture = null;
    
    // Frame Update
    onFrameUpdate: function () {
      if (script.backgroundTexture) {
        global.backgroundTexture = script.backgroundTexture;
      }
    }
    

Categories