using UnityEngine;
using System.Collections;
public class AlliedMovment : MonoBehaviour
{
Transform enemy;
PlayerHealth playerHealth;
EnemyHealth enemyHealth;
UnityEngine.AI.NavMeshAgent nav;
bool isDead;
void Awake()
{
enemy = GameObject.FindGameObjectWithTag("Enemy").transform;
nav = GetComponent();
}
void Update()
{
if (GameObject.FindGameObjectWithTag("Enemy") == isDead)
{
Destroy(enemy);
enemy = GameObject.FindGameObjectWithTag("Enemy").transform;
nav = GetComponent();
}
else
{
nav.SetDestination(enemy.position);
}
}
}
Hello, I'm a student with the assignment to modify the Survival Shooter Tutorial. I thought it would be cool to make some of the units friends with the player and help it fight. To make this happen I'm trying to make a script that tells a friendly unit to seek out an enemy, fight it, and then find a new enemy to repeat the process.
I have been trying to figure this out for the past two days and have made little progress. It technically works, except after a unit kills an enemy it will just stand there and will not seek a new target. The console returns an error:
"
> MissingReferenceException: The object> of type 'Transform' has been destroyed> but you are still trying to access it.> Your script should either check if it> is null or you should not destroy the> object.> UnityEngine.Transform.get_position ()> AlliedMovment.Update () (at> Assets/Scripts/Ally/AlliedMovment.cs:26)
"
I have tried to use **Destroy(gameObject);** to delete the reference, but I couldn't seem to implement it correctly. I also can't seem to figure out a way to tell the friendly unit to seek out the nearest enemy unit. They tend to chase what was closest to them at the time and chase it to the ends of the earth.
I have searched many topics with the same error message, but I have not had luck translating it into my own problem.
I clearly still have a lot to learn, so I was hoping if anyone could please explain what I'm doing wrong or suggest a better way to implement the code?
↧