I want a button to check a text, so once the text says X it's true, else you need to try again.

It may seems quite simple to do, but I'm not into javascript, I come from unity and C# and lose a bit on the logic process and references.

My code rn is:

//@input SceneObject TextoRespuestaInteraccion
//@input SceneObject OkayButton

// Add our onTrigger() function as a response to the custom trigger "my_trigger"
global.behaviorSystem.addCustomTriggerResponse("my_trigger", onTrigger);

function onTrigger() {
if(global.TextoRespuestaInteraccion === hola)
{
print("has ganado");
}
else
{
print("try again");
}

I have referenced my both SceneObjects in the inspector and I'm triggering this script from the behaviour on tap OkayButton and the response is: Send Custom Trigger.

But it seems it's not reading well the TextoInteraccion text! :(

Hope you can help me and I'm really grateful in advance,

Alejandro.

Best Answers

  • Bakari Mustafa
    Bakari Mustafa Posts: 174 πŸ”₯πŸ”₯πŸ”₯
    #2 Answer βœ“

    It looks like you’re trying to compare the value of global.TextoRespuestaInteraccion with the string "hola", but in your code, you’re comparing it with an undefined variable hola. Try changing the line if(global.TextoRespuestaInteraccion === hola) to if(global.TextoRespuestaInteraccion === "hola"). Also, make sure that the value of global.TextoRespuestaInteraccion is being set correctly before this function is called.

  • @Alexplorins

    Check if Target Text is True Script

    // -----JS CODE-----
    //@input SceneObject textObject //Add Text object
    //@input SceneObject checkButton //Add Image
    //@input string targetText // Type your Text you want to compare with
    
    // Bind the function to the touch event of the button
    if (script.checkButton) {
        var touchEvent = script.createEvent("TapEvent");
        touchEvent.bind(checkText);
    }
    
    function checkText() {
        var textComponent = script.textObject.getComponent("Component.Text");
        if (textComponent) {
            if (textComponent.text.includes(script.targetText)) {
                print("Correct!");
            } else {
                print("Try again.");
            }
        } else {
            print("Error: The textObject does not have a Component.Text component.");
        }
    }
    
    

    Bookmark for Reference

Answers

  • Thank you both! I now understand more the logical, thanks to fix my error on coding Bakari, and Joel your code enlightened me. Thank you :)