Okay so my goal is to find a gameobject with a tag “Player” and follow it. I am using a nav mesh with it and am finding it difficult to accomplish can anyone tell a how to fix the script to get it to work.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AI : MonoBehaviour {
public GameObject target;
public int speed;
public UnityEngine.AI.NavMeshAgent agent;
//Start
void Start () {
agent = GetComponent<UnityEngine.AI.NavMeshAgent>();
target = GameObject.FindWithTag("player");
}
//Update
void Update () {
agent.SetDestination(target.position);
if (gameObject.GetComponent<Enemy>().dead == true)
{
gameObject.tag = "Trash";
Destroy(agent);
Destroy(this);
}
}
}
Tags are case sensitive… So, before anything else, are you sure you’ve spelled it correctly?
Next, if this enemy is in the scene when it begins with the player, try instead of finding the target like you are, just drag and drop the player game object to this script and see how that goes & update the thread with your progress
This is actually what I tried however I am using a wave spawner script to spawn the enemies in so I have to edit the enemy prefab not just simply drag and drop it. Any other suggestions? And I made sure I spelled it right with all the caps.
Actually I just realized I didn’t capitalize the player tag in the script, so I changed it but it still doesn’t work. “position” in line 29 is underlined in red.
Oh, I see… okay, now that you mentioned that line, I missed something there.
It should tell you in the IDE what is wrong, ya? You want position, which is from ‘Transform’ not Gameobject.
Try editing it to read:
An alternative, for your spawning situation would be to reference the player in the spawner script. Then, pass that reference to any/every enemy that you spawn. That could avoid the GameObject.Find