Hello,
I am making a simple side scrolling game where I have some enemies that I want to start moving towards the player when they become visible on the screen. I am trying to use OnBecameVisible to start them moving but even when they are off screen, they start moving as soon as the game starts.
I am not sure what I am doing wrong because I have OnBecameInvisible to destroy them when they go off the screen and that works perfectly.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy_AI : MonoBehaviour
{
[SerializeField]
private float _enemySpeed;
void Start()
{
_enemySpeed = 7.0f;
enabled = false;
}
void Update()
{
Movement();
}
private void Movement()
{
if (enabled == true)
{
transform.Translate(Vector3.left * _enemySpeed * Time.deltaTime);
}
}
void OnBecameInvisible()
{
Destroy(gameObject);
}
void OnBecameVisible()
{
enabled = true;
}
}
Any help you can give me would be appreciated.
Many Thanks,
John