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
-
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 lineif(global.TextoRespuestaInteraccion === hola)
toif(global.TextoRespuestaInteraccion === "hola")
. Also, make sure that the value ofglobal.TextoRespuestaInteraccion
is being set correctly before this function is called.0 -
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
0
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
1