Q: Component.ScriptComponent
I am trying to build a simple script that creates a Component.ScriptComponent on all of the children objects which prints the name of the tapped object upon tapping on it. But instead of the name of the children it prints the name of the parent, what did I do wrong?
The script is set on the parent object which is ("Video"), so whenever I tapped on the block it should say : Cube_1_2 is Tapped rather than Video is Tapped.
Thanks for your help
here is my script
var thisObject = script.getSceneObject();
var l = thisObject.getChildrenCount();
function init(){
for(i=0;i<l;i++){
var obj = thisObject.getChild(i);
obj.createComponent("Component.InteractionComponent");
obj.createComponent("Component.ScriptComponent").createEvent("TapEvent").bind(function(){
print(script.getSceneObject().name + " is tapped!")
})
}
}
init();
You are printing script.getSceneObject()'s name, not the child object's name. I don't think the script object in the bound function refers to your child object; I think it is still the script object of your script. If you change the print statement to print(obj.name + " is tapped!") I think that will work. Or obj.getSceneObject or something.
I tried it, the first option print(obj.name + " is tapped!"), but it returns the last index object name instead of the tapped object?
I tried your code and it is due to closures. A helper function to create the touch component stuff makes the code work as you intend.
modelsbymike3d , Thank you so much sir! I discover and understand because of your reponse to me.