// Update is called once per frame void Update() { if (chomping && target != null) { CarrotStatus cs = target.GetComponent <CarrotStatus>(); cs.timeLeft -= Time.deltaTime; if (cs.timeLeft <= 0) { pickTarget(); if (target == null) { //game over man } } } }
private GameObject pickTarget() { chomping = false; bool targetPicked = false; int tries = 0; while (!targetPicked && tries < carrots.Length + 1) { int index = Random.Range(0, carrots.Length - 1); GameObject picked = carrots[index]; CarrotStatus status = picked.GetComponent <CarrotStatus>(); if (status.timeLeft != 0) { targetPicked = true; return(picked); } tries++; } return(null); }