player's pet attack.

have a script that spawns NPC’s and uses the FindGameObjectWithTag reference to follow the player, now i need these NPC’s to attack enemys. is there a way to look at another tag for a time while being able to come back to the original?

these NPC’s act like guard dogs.

You can store a reference to the player, and have a different reference for enemies. If there are any enemies around, attack them, but if not (or if the player moves too far), follow the player. You don’t need to get rid of the player reference to do this.

how would i go about doing this?
how do you store a tag?

You store the reference.

You could it like this:

private var player_hero : GameObject;

function Start()
{
    player_hero = GameObject.FindWithTag("Player");
}

Also, you don’t want to search for tags the whole time. The only enemies that are relevant to the NPCs are the ones that are within their range. Just add a collider on them and check for tags on collision.

how are you currently following the player?

You shouldn’t be calling FindGameObjectWithTag every frame - you should store the result in a GameObject Variable that you can reference later.

There are certainly other/better ways, but for the sake of simplicity, an example:

private GameObject playerObject;

void Start(){
    playerObject = GameObject.FindWithTag("player");
}

void Update (){
//code to follow player, using the variable playerObject

}

Try posting your code that the AI pet is using to follow the player around, and you can move from there to get it to chase down enemies.

so button makes goul, goul normally follows player, but when enemy is close… go attack enemy. im almost there.

using UnityEngine;
using System.Collections;

public class Goul : MonoBehaviour {
public Transform target;
public Transform player;
public int moveSpeed;
private Transform goul;
public int idleDistance = 1;
public int enemyDistance = 10;

void Awake(){
goul = transform;
}

void Start () {
GameObject to = GameObject.FindGameObjectWithTag(“Enemy”);
target = to.transform;
GameObject go = GameObject.FindGameObjectWithTag(“Player”);
player = go.transform;

}

void Update (){
Vector3 theScale = transform.localScale;
theScale.x *= -1;
transform.localScale = theScale;

}

void FixedUpdate () {
if (Vector3.Distance (goul.position, target.position) < enemyDistance) {
if (target.position.x < goul.position.x) {
goul.position -= goul.right * moveSpeed * Time.deltaTime; // target is left of goul, move left
}
else if (target.position.x > goul.position.x) {
goul.position += goul.right * moveSpeed * Time.deltaTime; // target is right of goul, move right
}
}
if (Vector3.Distance (goul.position, player.position) > idleDistance) {
if (player.position.x < goul.position.x) {
goul.position -= goul.right * moveSpeed * Time.deltaTime; // player is left of goul, move left
}
else if (player.position.x > goul.position.x) {
goul.position += goul.right * moveSpeed * Time.deltaTime; // player is right of goul, move right
}
}

Debug.DrawLine(player.position, goul.position, Color.yellow);

}

}

so i got them to follow the enemy after hitching a ride with the player, but when the enemy is destroyed it gives me an error.

“object of type ‘Transform’ has been destroyed but you are still trying to access it”

how do i do a check, or how do i ask it if there are any enemies left, and revert back to player as the follow target?

using UnityEngine;
using System.Collections;

public class Goul : MonoBehaviour {
	public Transform target;
	public Transform player;
	public int moveSpeed;
	private Transform goul;
	public int idleDistance = 1;
	public int enemyDistance = 10;

	void Awake(){
		goul = transform;
	}
	
	void Start () {
		GameObject to = GameObject.FindGameObjectWithTag("Enemy");
		target = to.transform;
		GameObject go = GameObject.FindGameObjectWithTag("Player");
		player = go.transform;
		
		
	}

	void Update (){ 
			Vector3 theScale = transform.localScale;
				theScale.x *= -1;
				transform.localScale = theScale;

		}

	void FixedUpdate () {
		if (Vector3.Distance (goul.position, target.position) < enemyDistance) {
						if (target.position.x < goul.position.x) {
								goul.position -= goul.right * moveSpeed * Time.deltaTime; // target is left of goul, move left
						} 
						else if (target.position.x > goul.position.x) {
								goul.position += goul.right * moveSpeed * Time.deltaTime; // target is right of goul, move right
						}
			}

		else if (Vector3.Distance (goul.position, player.position) > idleDistance) {
						if (player.position.x < goul.position.x) {
								goul.position -= goul.right * moveSpeed * Time.deltaTime; // player is left of goul, move left
						}
						else if (player.position.x > goul.position.x) {
								goul.position += goul.right * moveSpeed * Time.deltaTime; // player is right of goul, move right
						}
				}

		
		Debug.DrawLine(player.position, goul.position, Color.yellow);
		Debug.DrawLine(target.position, goul.position, Color.green);
	

}

}

Try checking to see if the target is null. If it is, then don’t try accessing its properties and just go for the player. You should also have some logic checking the player as well, as you will get the same error if your player dies.

You can set up some sort of enemy manager class that keeps track of the number of enemies in the scene, When an enemy dies, notify that class, and you can reference the class with your goul to know how many enemies are left.

how in the world would you check if something is null when its a parameter? o.O

i was thinking of setting up a list of enemies, but how would i tell the script “enemies are gone bro, go back to player.”

i can’t check right now, but i believe that

(target == null)

should return true once the object is destroyed.

If your target is null, you need to either find another target, or stop referencing it and just follow your player until you have a target again.

thank you