iTween - onCompleteTarget not working for me

Hi all!
I’m trying to make a game object fade out using iTween and then execute a function. I’ve spent a good few hours looking at various posts regarding how to use onCompleteTarget and onComplete for iTween but no matter what I do, my “Teleporter” function is not executed. This is what I’m doing:

iTween.FadeTo(Robot,{"alpha":0, "time":3, "onCompleteTarget":gameObject, "onComplete":"Teleport"});

“Teleport” is a function in the same Javascript:

function Teleport () {
	Debug.Log("Teleported...");
}

I thought all I had to do was to specify the current object (“gameObject”) and the callback would look for the function on the current game object but nothing happens! It works fine if I remove onCompleteTarget and place a temp script on the “Robot” game object but I’d rather keep everything together if possible.

Does anyone have any ideas of what I’m doing wrong?

Thanks!

iTween.FadeTo(Robot,{“alpha”:0, “time”:3, “oncompletetarget”:gameObject, “oncomplete”:“Teleport”});

Hi!

Thank you for your reply and suggestion. Unfortunately, it didn’t work :-(. I think I tried the different cases before too after having read about it somewhere. I tried it again now though just in case but it still doesn’t work. Do you (or anyone else) have any other suggestions?

Much appreciated!

iTween.FadeTo(Robot,{"alpha":0, "time":3, "onCompleteTarget":Robot.gameObject, "onComplete":"Teleport"});

Try this?

[EDIT] Nope sorry, that’s completely wrong… hold on a sec

Ok, this should work

iTween.FadeTo(Robot,{"alpha":0, "time":3, "onCompleteTarget":this.gameObject, "onComplete":"Teleport"});

Something to do with certain object types being set as the target of the animation by default.

[FURTHER EDIT] But you may not want to do that. Instead I’d put your “Teleport” function on your Robot’s script and use my first solution, which is to say, OnComplete get the robot that teleported to tell me he has teleported, rather than the script the animation is called from… but that’s entirely up to you :wink:

Thank you but that didn’t seem to work either… I experimented with putting the script on the Robot object before and that works fine but it’s a little awkward in the context, so I’d rather not do it if I can avoid it.

At this point though, I feel like I would mostly like to just understand how the onComplete and onCompleteTarget parameters work since I think I’ll need them later on in any case.

Do you have any other suggestions on what could be wrong? Thanx!

I think you want onCompleteTarget to be “this”, not “gameObject”, since the component is actually the one which has the function in question.

I tried but unfortunately that gives me an error: “InvalidCastException: Cannot cast from source type to destination type.” when the fade is finished. Sorry, but thanks for trying to help me!

Whoops, I was remembering it wrong, it is “gameObject”. I’m using that in a few places without issue, and the only differences I can see are that mine is entirely lower-case - “oncompletetarget”, and using C#; not sure whether either of those matters. For example, this works fine:

iTween.MoveTo(_blocks[i], iTween.Hash(
			"time", 			1.5f,
			"y", 				-2.0f,
			"islocal",			true,
			"easetype",			"linear",
			"oncomplete", 		"Sunk",
			"oncompletetarget",	gameObject,
			"oncompleteparams", i));
...
void Sunk ( object obj )
{
 ...
}

OK, I think this has something to do with the “SendMessage” function. By inserting some Debug.Log statements into iTween.cs itself I can see that my original code actually references the correct callback object. BUT when I removed the SendMessageOptions.DontRequireReceiver parameter from the SendMessage call I get:

SendMessage Teleport has no receiver!

I tried just doing this:

SendMessage("Teleport");

from the same place that I have my original iTween.FadeTo line and it gives the same error message. I also created another script, pasted my Teleport function into it and placed it on the same game object but I still get the same error :(.

Is there something I’m not doing right with SendMessage (though I’m sure iTween is doing it “right” somehow)? Thanx!

That is strange. I converted it to C# and it works fine, note the use of the this keyword is referencing the FadeTo class’ function called “Teleport”.

Things to note:

  • Using gameObject is important as that is the object type required by iTween to access references.
  • Using lowercase/camelCase does not matter, all strings are cast to lower case by iTween before being compared. Using camelCase makes it much easier to read.
using UnityEngine;
using System.Collections;

public class FadeTo : MonoBehaviour {
	
	public GameObject Robot;
	
	// Use this for initialization
	void Start () {
		iTween.FadeTo(Robot.gameObject, iTween.Hash("alpha", 0.0f, 
													"time", 3.0f, 
													"onCompleteTarget", this.gameObject, 
													"onComplete", "Teleport"));
	}
	
	// Update is called once per frame
	void Update () {
	
	}
	
	void Teleport() {
		Debug.Log("Teleported..");		
	}
}

Attach this class to your camera and drag and drop your ‘Robot’ object onto the inspector. Maybe this is something to do with the java script implementation of iTween?

Thanks! I’ll try this when I get home. I’ll have to dust off my C# skills!

Hi again!

Thanks again for your sample - it worked! And it made me realise what the problem was. I tried placing my original script in Start() on the camera too and then my script worked as well. There must be something “wrong” with the object the script was originally attached to which was an NGUI button.

In any case, now that I’ve realised that I can work around the issue (or perhaps debug the button). Thanks again to everyone who posted!