{"id":815,"date":"2011-02-21T22:23:10","date_gmt":"2011-02-21T22:23:10","guid":{"rendered":"http:\/\/pixelplacement.com\/?p=597"},"modified":"2017-01-12T04:47:58","modified_gmt":"2017-01-12T04:47:58","slug":"understanding-itween-callbacks","status":"publish","type":"post","link":"https:\/\/www.pixelplacement.com\/site\/2011\/02\/21\/understanding-itween-callbacks\/","title":{"rendered":"Understanding iTween Callbacks"},"content":{"rendered":"<p>One of the most frequent problems I see people have with iTween is with callbacks that don&#8217;t fire.<\/p>\n<p>At it&#8217;s core iTween leverages Unity&#8217;s <a href=\"http:\/\/unity3d.com\/support\/documentation\/ScriptReference\/GameObject.SendMessage.html\" target=\"_blank\">SendMessage<\/a> method for carrying out it&#8217;s 3 callbacks: &#8220;onStart&#8221;, &#8220;onUpdate&#8221; and &#8220;onComplete&#8221;. When you add an iTween to a GameObject, iTween will throw all callbacks against the GameObject that is being animated. Where most people have difficulties is when they assign iTweens to GameObjects that don&#8217;t physically contain the methods they are attempting to call.<\/p>\n<p><!--more-->For example, the following code with never execute the &#8220;ShakeComplete&#8221; method because the &#8220;target&#8221; GameObject does not have a script attachted to it containing this method:<\/p>\n<pre>using UnityEngine;\r\nusing System.Collections;\r\n\r\npublic class UnderstandingCallbacks : MonoBehaviour\r\n{\r\n\tpublic GameObject target;\r\n\t\r\n\tvoid Start ()\r\n\t{\r\n\t\tiTween.ShakePosition(target, iTween.Hash(\"x\", 1, \"onComplete\", ShakeComplete));\r\n\t}\r\n\t\r\n\tvoid ShakeComplete(){\r\n\t\tDebug.Log(\"The shake completed!\");\t\r\n\t}\r\n}\r\n<\/pre>\n<p>There&#8217;s 2 ways to correct the previous script. The long route: create another script, attach it to our actual &#8220;target&#8221; GameObject and transfer the &#8220;ShakeComplete&#8221; method to that new script. The easy route: utilize iTween&#8217;s callback &#8220;target modifiers&#8221;!<\/p>\n<p>Each of iTween&#8217;s 3 callbacks have an additional &#8220;target modifier&#8221; that can be used to tell iTween to look somewhere else for the callback method: &#8220;onStartTarget&#8221;, &#8220;onUpdateTarget&#8221; and &#8220;onCompleteTarget&#8221;.<\/p>\n<p>The following corrected script will now fire the &#8220;onComplete&#8221; callback since we are now using &#8220;onCompleteTarget&#8221; to tell iTween that the &#8220;ShakeComplete&#8221; method is located on the GameObject that actually set up the iTween:<\/p>\n<pre>using UnityEngine;\r\nusing System.Collections;\r\n\r\npublic class UnderstandingCallbacks : MonoBehaviour\r\n{\r\n\tpublic GameObject target;\r\n\t\r\n\tvoid Start ()\r\n\t{\r\n\t\tiTween.ShakePosition(target, iTween.Hash(\"x\", 1, \"onComplete\", \"ShakeComplete\", \"onCompleteTarget\", gameObject));\r\n\t}\r\n\t\r\n\tvoid ShakeComplete(){\r\n\t\tDebug.Log(\"The shake completed!\");\t\r\n\t}\r\n}\r\n<\/pre>\n<p>I hope this helps clear up unsuccessful callbacks in iTween.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the most frequent problems I see people have with iTween is with callbacks that don&#8217;t fire. At it&#8217;s core iTween leverages Unity&#8217;s SendMessage method for carrying out it&#8217;s 3 callbacks: &#8220;onStart&#8221;, &#8220;onUpdate&#8221; and &#8220;onComplete&#8221;. When you add an iTween to a GameObject, iTween will throw all callbacks against the GameObject that is being [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1447,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[25],"_links":{"self":[{"href":"https:\/\/www.pixelplacement.com\/site\/wp-json\/wp\/v2\/posts\/815"}],"collection":[{"href":"https:\/\/www.pixelplacement.com\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pixelplacement.com\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pixelplacement.com\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pixelplacement.com\/site\/wp-json\/wp\/v2\/comments?post=815"}],"version-history":[{"count":2,"href":"https:\/\/www.pixelplacement.com\/site\/wp-json\/wp\/v2\/posts\/815\/revisions"}],"predecessor-version":[{"id":1497,"href":"https:\/\/www.pixelplacement.com\/site\/wp-json\/wp\/v2\/posts\/815\/revisions\/1497"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pixelplacement.com\/site\/wp-json\/wp\/v2\/media\/1447"}],"wp:attachment":[{"href":"https:\/\/www.pixelplacement.com\/site\/wp-json\/wp\/v2\/media?parent=815"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pixelplacement.com\/site\/wp-json\/wp\/v2\/categories?post=815"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pixelplacement.com\/site\/wp-json\/wp\/v2\/tags?post=815"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}