Answer the question
In order to leave comments, you need to log in
How to determine in the unit that the GameObject has been destroyed in order to exclude interaction with it?
The game should constantly spawn clones of enemies and clones of allies
. I make an enemy AI and as soon as it destroys an ally, an error occurs: MissingReferenceException: The object of type 'GameObject' 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.
I tried checking for null, but it doesn't help.
public class EnemyAI : MonoBehaviour {
public float speed;
public float mSpeed;
private float waitTime;
public float StartWaitTime;
float angle;
float mvalue;
float slopvalue;
float rotZ;
public float rotationOffset;
public GameObject bullet;
public Transform firePoint;
public float intervalpershoot;
private float updatetime;
public bool Agressive = false;
GameObject[] friend;
GameObject closest;
public float MaxSpread;
public float MinSpread;
void Start()
{
friend = GameObject.FindGameObjectsWithTag("Friend");
waitTime = StartWaitTime;
}
void Update() {
updatetime -= Time.deltaTime;
if (waitTime <= 0)
{
angle = Random.Range(-360, 360);
mvalue = Random.Range(0, 2);
slopvalue = Random.Range(1, 3);
waitTime = StartWaitTime;
}
else
{
waitTime -= Time.deltaTime;
}
if (Agressive == true)
{
var near = FindClosest();
Vector3 difference = near.transform.position - transform.position;
rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
Quaternion rotation = Quaternion.AngleAxis(rotZ + rotationOffset, Vector3.forward);
transform.rotation = Quaternion.Lerp(transform.rotation, rotation, Time.deltaTime * speed);
}
if (mvalue == 1 && Agressive == false)
{
transform.Translate(-Vector2.left * mSpeed * Time.deltaTime);
}
else if (Agressive == false)
{
Quaternion Rot = Quaternion.Euler(0, 0, angle);
transform.rotation = Quaternion.Slerp(transform.rotation, Rot, Time.deltaTime * speed);
}
}
void Slope()
{
if (slopvalue == 1 && Agressive == true)
{
transform.Translate(Vector2.up * mSpeed * Time.deltaTime);
}
else if (slopvalue == 2 && Agressive == true)
{
transform.Translate(-Vector2.up * mSpeed * Time.deltaTime);
}
}
void Shoot()
{
if (updatetime <= 0f)
{
var rotationY = Quaternion.AngleAxis(Random.Range(MinSpread, MaxSpread), transform.up);
var rotationX = Quaternion.AngleAxis(Random.Range(MinSpread, MaxSpread), transform.right);
var bruh = rotationX * rotationY;
var bruh2 = rotationY * rotationX;
Instantiate(bullet, firePoint.position, firePoint.rotation * bruh * bruh2);
updatetime = intervalpershoot;
}
}
void OnTriggerEnter2D()
{
Agressive = true;
}
void OnTriggerExit2D()
{
Agressive = false;
}
GameObject FindClosest()
{
float dist = Mathf.Infinity;
Vector3 pos = transform.position;
foreach (GameObject go in friend)
{
Vector3 diff = go.transform.position - pos; //ошибка указывает сюда
float curDis = diff.sqrMagnitude;
if (curDis < dist)
{
closest = go;
dist = curDis;
}
}
return closest;
}
}
Answer the question
In order to leave comments, you need to log in
Well, you are trying to access a destroyed gameobject, so either you need to remove it from the friend array when destroying it, or it can help to get this array every time, maybe it will not look for deleted ones like this
...
friend = GameObject.FindGameObjectsWithTag("Friend");
foreach (GameObject go in friend)
....
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question