I have a game with a bunch of different types of fish, all the fish’s(fishes’?) scripts are inheriting from one base script. I have a part in that script that disables the object if it leaves the screen for too long.
I can’t really put my finger on what’s going on, but so far I only have 2 different types of fish. One is being deactivated after leaving the screen for 5 seconds, as intended. The other is not.
Here’s my base script with the aforementioned method.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class EnemyFish : MonoBehaviour
{
public float speed, size, detectRange;
public Vector3 moveGoal;
protected Animator anim;
public bool isLeft;
public int fishNum;
private Camera cam;
Vector2 screenHalfSize;
float timerToDeactivate = 5;
private void Awake()
{
cam = Camera.main;
screenHalfSize = new Vector2(Camera.main.aspect * Camera.main.orthographicSize, Camera.main.orthographicSize);
}
private void Update()
{
if(transform.position.x > screenHalfSize.x + 5 || transform.position.x < -screenHalfSize.x -5 || transform.position.y > screenHalfSize.y || transform.position.y < -screenHalfSize.y)
{
timerToDeactivate -= Time.deltaTime;
if(timerToDeactivate < 0)
{
timerToDeactivate = 5;
gameObject.SetActive(false);
}
}
else if(timerToDeactivate < 5)
{
timerToDeactivate = 5;
}
}
private void OnDisable()
{
FishSpawner.Spawner.spawnNumbers[fishNum] -= 1;
}
}
I even tried watching the “timerToDeactivate” after setting the inspector to debug mode, I can’t, for the life of me, figure out what’s wrong. Both of the fish’s scripts inherit from “EnemyFish”, I am sure of this as they both use the inherited moveGoal and speed variables. I even tried dragging the fish outside of the screen and holding it there, the timer does not go down, but if I try it with the other fish it works fine.
Any help would be appreciated, thanks.