Hi, I’m trying to make an enemy gameobject gather instantiated objects. But, i get a null reference exception because the object is destroyed after being gathered. The object is removed from the hierarchy. But, it’s still selected as a pre fab. If i do / if( object != null)/ the enemy doesn’t move towards the objects .So, it either doesn’t move if i put a null exception or it does move but then i get an error because the game object has been destroyed. What do I do?
public class EnemyScript : MonoBehaviour
{
void Update()
{
if (SphereObject == null)
{
SphereObject = GameObject.FindGameObjectWithTag("square").GetComponent<ColliderDestroy>();
}
if (SphereObject != null)
{
transform.position = Vector2.MoveTowards(transform.position, SphereObject.transform.localPosition, 2 * Time.deltaTime);
}
public class PushButton : MonoBehaviour
{
[SerializeField] GameObject[] arrayof;
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.tag == "Player")
{
instan();
}
}
public void instan()
{
Instantiate(arrayof[0], new Vector2(Random.Range(-4, 4),Random.Range(-4,4)), Quaternion.identity);
}