Spawn method for an Unkown Gameobject

Hi
Been trying to create a Spawn() method for a Spawener GameObject that receive a GameObject tag as a parametr. Although when I run a test I get an Error message ArgumentException: The Object you want to instantiate is null.

spawner code:

public class SpawnerScript : MonoBehaviour
{
    
    public void Spawn (int amount , float span, string enemyTag)
    {
        
        GameObject Enemy;
        Enemy = GameObject.FindGameObjectWithTag(enemyTag);
        Debug.Log(enemyTag);
        Debug.Log (Enemy);
        float interval = amount / span;
        for (int i = amount/-2 ; i <= amount/2; i++)
        {
            Instantiate(Enemy,new Vector3(transform.position.x + interval*i,transform.position.y,transform.position.z), Quaternion.identity);
        }
    }
}

and here is the script that call it:

public class LogicScript : MonoBehaviour
{
    public SpawnerScript Spawner;

    int amount =3;
    float span = 10;
    string enemyTag = "Enemy1";
    // Start is called before the first frame update
    void Start()
    {
        Spawner.Spawn(amount, span, enemyTag);
    }
    
    // Update is called once per frame
    void Update()
    {

    }
}

First, remove the clutter from your scripts.

Inline your declarations :

GameObject Enemy = GameObject.FindGameObjectWithTag(enemyTag);

Remove unused Update method.

The problem is that :

Enemy = GameObject.FindGameObjectWithTag(enemyTag);

Returns null. It can’t find a game object with the tag enemyTag.

Possible solution : Add a game object in the scene with the given tag. FindObjectWithTag doesn’t work with prefabs that aren’t in the scene.