I have a game where a Gunner moves on a pre-set path towards a final destination. You play as a Magic Bullet that auto-fires from the Gunner's current position and can be controlled with WASD. When the bullet collides with a Target, it's destroyed.

My question: How do I change the speed of the Gunner after all of the Targets are destroyed? The idea if for the Gunner to hurry to the end, instead of skilled players waiting around.

Any help would be greatly appreciated. Here's what I have so far:

using UnityEngine;
using System.Collections;

public class MovementPathScript : MonoBehaviour {

    public GunnerPathScript PathToFollow; 
    public int CurrentWayPointID = 0;

    private float speedVariable;
    public float gunnerSpeed;
    public float hurrySpeed; 

    private float reachDistance = 1.0f;
    public float rotatationSpeed = 5.0f;

    public string pathName;

    public static Quaternion gunnerRotation;

    private Vector3 last_position;
    private Vector3 current_position; 

    void Start () {
        last_position = transform.position; 

    void Update () {

        float distance = Vector3.Distance (PathToFollow.path_objs [CurrentWayPointID].position, transform.position); 
        GunnerMove ();

        PointTowards (); 

        if (distance <= reachDistance) {

        if (CurrentWayPointID >= PathToFollow.path_objs.Count) {
            CurrentWayPointID = 13;

        void GunnerMove(){
            transform.position = Vector3.MoveTowards (transform.position, PathToFollow.path_objs [CurrentWayPointID].position, Time.deltaTime * speedVariable);  
            speedVariable = gunnerSpeed; 

        if (GameObject.FindGameObjectsWithTag ("Target") == null) {  
            speedVariable = hurrySpeed;  
            //Unity doesn't recognize this, and maintains gunnerSpeed


    void OnTriggerEnter(Collider other){
        if (other.gameObject.CompareTag ("Target"))
            Destroy (this.gameObject);

    public void PointTowards(){ 
        var gunnerRotation = Quaternion.LookRotation (PathToFollow.path_objs [CurrentWayPointID].position - transform.position);
        transform.rotation = Quaternion.Lerp(transform.rotation, gunnerRotation, Time.deltaTime * rotatationSpeed); 
  • \$\begingroup\$ If you are not using a List to determine target then at least implement some delegates that will notify this script when target destroys. In that listener (delegate) you can check if (GameObject.FindGameObjectsWithTag ("Target").Length == 0) as mentioned in answer below. But please do not use GameObject.FindGameObjectsWithTag in Update as it finds in whole hierarchy and you are executing it in Update \$\endgroup\$ – Hamza Hasan Apr 7 '16 at 11:51
  • \$\begingroup\$ Thanks for the tip. I'm still learning, so this seems like a great opportunity to learn delegates and events. \$\endgroup\$ – Tony Chacon Apr 7 '16 at 15:12
  • \$\begingroup\$ Have a look at this answer :) \$\endgroup\$ – Hamza Hasan Apr 7 '16 at 15:13

Change this line:

if (GameObject.FindGameObjectsWithTag ("Target") == null) {  


if (GameObject.FindGameObjectsWithTag ("Target").Length == 0) {

Reason: FindGameObjectsWithTag returns an array, it can never be null, but it can be "empty". So you need to check it's length.

Also note that this is an inefficient way of target tracking. It would be better if you keep a list of targets somewhere and check the count of that list.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.