FindGameObjectsWithTag?

Hey everyone,

In the project that I’m currently working on I have a Player and a Deer object and I want the Deer to move away from the Player when the player is a certain distance away from the deer. Here is the script I have been using which works fine:

using UnityEngine;
using System.Collections;

public class Run : MonoBehaviour {
    public Transform target;
    public float speed;
    public GameObject player ;
    public GameObject RedDeer;
    public float distance ;

   
   
    // Use this for initialization
    void Start () {
       
    }
   
    // Update is called once per frame
    void Update () {
        player = GameObject.FindGameObjectWithTag ("Player");
        RedDeer = GameObject.FindGameObjectWithTag ("RedDeer");
        distance = Vector3.Distance(player.transform.position , RedDeer.transform.position);
       
    if (distance < 100 ){

            {
        float step = speed * Time.deltaTime;
            transform.position = Vector3.MoveTowards(transform.position, target.position, step);

        }
   
           
        }
    }
}

I attach this to the Deer Object and assign the tags appropriately. This works great, the Deer moves away from the player once the Player gets too close. Now I want this to work with multiple deer objects. I’ve tried switching “FindGameObjectWithTag” to “FindGameObjectsWithTag” but than I get an error saying:
Cannot implicitly convert type UnityEngine.GameObject[ ]' to UnityEngine.GameObject’.

How can I solve this so that I can have multiple deer objects who will all work with this script? Any help would be greatly appreciated.

in this case… well I just say what I’m thinking right now… I personally don’t like any part of the current script.
What is this script attached to? It’s hard to say since it tries to grab a reference to both the player and the deer…so it’s not attached to either? Grabbing things using find in Update is a no no (search the forums for it and see the shame it has brought on people)

That being said. In this instance I’d use simple triggers on your deer set to the size of the detection range. If the player enters this trigger…spook the deer.

edit: a little more to go on;

and

bool spooked = false;

void OnTriggerEnter(Collider other) {
       if (other.gameObject.CompareTag("Player")){
              // set a bool here to start running away
              spooked = true;
       }
    }

// your run away function
void Update(){
     if(spooked){
           float step = speed* Time.deltaTime;
           transform.position = Vector3.MoveTowards(transform.position, target.position, step); //probably change this out at somepoint too use something like (player.transform.position - transform.position) to run away from player instead of to some predetermined point
      }
}
// then use on trigger exit to stop running away
2 Likes

@novashot that would only work if he’s not using the trigger collider for something else already.

To make it work you’ll need to do move “RedDeer= GameObject.FindGameObjectWithTag(“RedDeer”);” to the start function and replace it with this:

RedDeer = gameObject;

Attach the script to your game object and make sure you apply it to all. Then should work fine. (Although it could use some optimizing.)

1 Like

FindGameObjectsWithTag() returns an array, so you can’t have it equal to one game object, you have to use a loop:

GameObject[] deer = GameObject.FindGameObjectsWithTag("RedDeer");
for each(GameObject d in deer){
distance = getDistanceFromPlayer(d);
if( distance < minDistance)StartCoroutine(moveDeer(d));
}
2 Likes

This is exactly what I was looking for, thanks so much for your help.