OnMouseDown() problem

Hello, I am making 2D game, but stuck with simple problem: I have cube game object, with material on it with changing textures over time. It works fine, but now I need to use OnMouseDown() function to destoy this cube, here is my code:

var duckSpeed: int;

private var directionX: int = 1;
private var directionY: int = 1;

private var tempTimer: float = 0;
private var animTimer: float = 0;

private var isDead: boolean = false;

var duckAnim: Texture2D[];

function Update () {	
	
	if(tempTimer < 8)
		duckFlying();
	else
		duckFlyAway();
	
	tempTimer += Time.deltaTime;

	if(isDead){
		Destroy(gameObject);
	}
}

function OnMouseDown()
{
	isDead = true;	
}

function duckFlyAway()
{
	if(isDead)
		return;
//some code	
	
}

function duckFlying()
{
	if(isDead)
		return;
		
	amtToMove = duckSpeed * Time.deltaTime;
	
	if(animTimer > 0.5){
		renderer.material.SetTexture("_MainTex", duckAnim[0]);
		if(animTimer >= 1)
			animTimer = 0;
	}
	
	if(animTimer < 0.5){
		renderer.material.SetTexture("_MainTex", duckAnim[1]);		
	}
	
	if(transform.position.y >= 14)
		directionY = -1;
	
	if(transform.position.y <= -6)
		directionY = 1;
	
	if(transform.position.x  >= 20)
	{
		directionX = -1;
		renderer.material.SetTextureScale ("_MainTex", Vector2( 1, -1));
	}
	
	if(transform.position.x <= -20.5)
	{	
		directionX = 1;
		renderer.material.SetTextureScale ("_MainTex", Vector2( -1, -1));
	}
	
	transform.Translate(Vector3.up * amtToMove * directionY);	
	transform.Translate(Vector3.right * amtToMove * directionX);	
	
	animTimer += Time.deltaTime;
}

I cant delete my cube immidiattely, only after 3-6 seconds, dont know why :frowning: , please help anyone

I think trying to hack events around Update is confusing…how about:

var duckAnim : Texture2D[];
var animSpeed = 2.0;
var duckSpeed = 5.0;
var duckTimer = 8.0;

function Start () {
	StartCoroutine("DuckFlying");
	yield WaitForSeconds(duckTimer);
	StopCoroutine("DuckFlying");
	DuckFlyAway();
}

function OnMouseDown() {
	Destroy(gameObject);
} 

function DuckFlyAway() {
//some code 
} 

function DuckFlying() {
	var directionX = 1.0;
	var directionY = 1.0;
	while (true) {
		transform.Translate(Vector3(directionX, directionY, 0.0) * duckSpeed * Time.deltaTime);
		var pos = transform.position;
		if (pos.y >= 14.0 || pos.y <= -6.0)
			directionY = -directionY;
		if (pos.x >= 20.0 || pos.x <= -20.5) {
			directionX = -directionX;
			renderer.material.mainTextureScale.x = -renderer.material.mainTextureScale.x;
		}
		transform.position = Vector3(Mathf.Clamp(pos.x, -20.5, 20.0), Mathf.Clamp(pos.y, -6.0, 14.0), pos.z);
		renderer.material.mainTexture = duckAnim[parseInt(Time.time * animSpeed) % duckAnim.Length];
		yield;
	}
}

Also you can have an arbitrary number of animation frames using this method.

–Eric

Thanks! But now I have this message:

“Coroutine ‘DuckFlying’ couldn’t be started!
UnityEngine.MonoBehaviour:StartCoroutine(String, Object)
UnityEngine.MonoBehaviour:StartCoroutine(String, Object)
UnityEngine.MonoBehaviour:StartCoroutine(String)
$:MoveNext() (at Assets\Scripts\duckScript.js:31)”

Usually get that error if you have a typo…function names need to match exactly.

–Eric