itween: targeting differnt gameobjects after mousecklick

Hey guys. Iam trying to archive the following thing:

i have 2 boxes and one character which is controlled with the third person controller in my game.
and all i want to do is, that the character will move automaticly to the box when i clicked it.

i tried it with itween (visualEditor). works verry well with one box. but only when marked it as “play automaticly” because i have no idea how i call the itween function only when i click one box. so, this would be my first question:

how can i call the itween function when i click on the box?

second question:
i have two boxes. and at the moment, the itween script is put onto the player. so that means if i put 20 boxes in the game. there have to be 20 itween scripts in the player object? isnt there a better solution for this?

thanks for help

You do not need 20 iTween scripts, and you do not need to use the visual iTween editor.

Assuming you know how to communicate between two different game objects and to send variables between them, the basic logic for such implementation is simple but may vary depending on your situation.

In general: The player object should listen to tap events on the box, when any box is tapped, the player decides what to do with it.

You can implement it with C# events, with Unity’s SendMessage, or using publicly exposed methods on the player.

Example for the latter:

// In player script
public void OnBoxTap( GameObject box ) {
	iTween.MoveTo( gameObject, iTween.Hash(
		"position", box.transform.position,
		"time"    , 1
	));
}

// In box script
public PlayerClass player;

void OnMouseDown() {
	player.OnBoxTap( box );
}