Basic problem with instantiated prefabs

Hi, i’m new to Unity and going through a Unity Learn tutorial. This code works for one thing, but not for another, very similar thing.

I have a Food prefab that’s instantiated by a PlayerController script attached to the player, and each instantiated object is destroyed when it goes out of bounds by a script attached to the Food prefab, called DestroyOutOfBounds. It works fine- when I press the space bar, food spawns, and when it goes out of bounds, it’s destroyed, and I can spawn as many as I like with no issues, they all get destroyed out of bounds.

However, I have a list of animal prefabs, and when I press the s key, an animal is chosen and instantiated by a SpawnManager GameObject and its attached script, and each animal is supposed to be destroyed when they go out of bounds by the DestroyOutOfBounds script that’s also attached to the animals, but when an animal goes out of bounds, i can’t spawn any more- the error “MissingReferenceException: The object of type 'GameObject has been destroyed but you are still trying to access it.” comes up. I need to keep spawning animals, but this error doesn’t let me.

I think it’s destroying the entire animal prefab instead of the instantiated object, but I need it to destroy each instantiated object while being able to spawn more. It’s weird that this issue doesn’t come up with the Food prefab. It’s the same script, but it works for one and not the other.

Thanks in advance for any help i may get c:

SpawnManager:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SpawnManager : MonoBehaviour
{
    public GameObject[] animalPrefabs;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //Detect when s key is pressed
        if(Input.GetKeyDown(KeyCode.S))
        {
            // Choose animal from list
            int animalIndex = Random.Range(0, animalPrefabs.Length);
            // Instantiate new animal prefab
            Instantiate(animalPrefabs[animalIndex], new Vector3(0, 0, 20), animalPrefabs[animalIndex].transform.rotation);
        }

       
    }
}

PlayerController:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public float horizontalInput;
    public float speed = 20.0f;
    public float xRange = 10.0f;
    public GameObject projectilePrefab;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        // Stop player from moving out of bounds
        if(transform.position.x < -xRange)
        {
            transform.position = new Vector3(-xRange, transform.position.y, transform.position.z);
        }

        if (transform.position.x > xRange)
        {
            transform.position = new Vector3(xRange, transform.position.y, transform.position.z);
        }
        // Movement
        horizontalInput = Input.GetAxis("Horizontal");
        transform.Translate(Vector3.right * Time.deltaTime * speed * horizontalInput);

        // Detect spacebar press
        if (Input.GetKeyDown(KeyCode.Space))
        {
            // Launch sandwiches
            Instantiate(projectilePrefab, transform.position + new Vector3(0, 36, 0), projectilePrefab.transform.rotation);
        }
    }
}

DestroyOutOfBounds:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DestroyOutOfBounds : MonoBehaviour
{
    private float topBounds = 35.0f;
    private float bottomBounds = -10.0f;
    // Start is called before the first frame update
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        // Destroy object out of bounds (this script is applied to both food and animal prefabs- for some reason destroys instantiated food clones and lets me spawn more, but destroys animal prefabs (i think) and doesn't let me spawn more
        if (transform.position.z > topBounds)
        {
            Destroy(gameObject);
        }
        else if (transform.position.z < bottomBounds)
        {
            Destroy(gameObject);
        }
    }
}

Check your animalPrefabs. Maybe you accidentally dragged objects from scene to the inspector instead of prefab from your assets.