How can I access a GameObjects components using an instantiation variable?

I am trying to dynamically spawn enemies. All variables are declared and set prior to the instantiation code.

_enemy_1 = Zombie; (Zombie is a prefab.)
_zombie_level is a variable, which is in a Zombie script attached to its prefab.

_spawned_enemy_1 = Instantiate(_enemy_1, _enemy_1_spawn_position, Quaternion.identity);
Zombie _Zombie = _spawned_enemy_1.GetComponent<Zombie>();
_Zombie._zombie_level = _enemy_1_level;

This is what I have so far and it works fine. However, I don’t want to hard-code anything (if possible), which means not using the prefab name Zombie to GetComponent.

Is there a way I can access the _zombie_level variable using _spawned_enemy_1 as the GameObject? _spawned_enemy_1 is not a prefab, so I am receiving errors. I hope my explanation was clear and I appreciate any suggestions.

Thank you!

Doing it that way will only lead to bloating code and can become unmanageable as the number of enemies grows. Using the game object name to differentiate enemies is a terrible practice. If all you wanted to do is access level variable which all the enemies have, you can do this either through inheritence or by composition.

Inheritance Method

All enemies should inherit from the same class which contains the variable level. It would look something like this.

public class Enemy : Monobehaviour
{
       public int Level;
}

public class Zombie : Enemy {}

You can get the component to set the zombie’s level like this.

Enemy enemy = _spawned_enemy_1.GetComponent<Enemy>();
enemy.Level = _enemy_1_level;

You can check if it’s a zombie or not by doing something like this.

if(enemy is Zombie)
Debug.Log("This is a zombie.");

Composition Method

You can do the similar thing using interfaces. Add an interface called ILevelable to Zombie class. It would look something like this.

public interface ILevelable{
int Level {get;set;}
}

public class Zombie : Monobehaviour, ILevelable{
public int Level {get; set;}
}

You can just access the enemy via the interface like this as long as they contain this interface.

ILevelable enemy = _spawned_enemy_1.GetComponent<ILevelable>();
enemy.Level =  _enemy_1_level;

_spawned_enemy_1.getComponent<neededComponent>();. Dont forget to use an explicit typification when instatiat and object

Something like this might work. I believe unity will try and pull the first component it can find that is using the IZombie interface.

public interface IZombie
{
    int zombie_level { set; }
}

public class Zombie : MonoBehaviour, IZombie
{
    public int _zombie_level = 0;
    public int zombie_level { set => _zombie_level = value; }
}

public class mySpawner : MonoBehaviour
{
    GameObject _enemy_1;
    Vector3 _enemy_1_spawn_position;
    GameObject _spawned_enemy_1;

    public void SpawnMe(int _enemy_1_level)
    {
        _spawned_enemy_1 = Instantiate(_enemy_1, _enemy_1_spawn_position, Quaternion.identity);
        var _Zombie = _spawned_enemy_1.GetComponent<IZombie>();
        _Zombie.zombie_level = _enemy_1_level;
    }
}

Thanks for all for the suggestions. However, they weren’t quite what I was looking for. I didn’t want any reference at all to Zombie or to hard code it. I was only able to get it to work using a function, which contained a switch if the _spawned_enemy_1.name matches, so it would still work dynamically.

Get_Enemy_Component(_spawned_enemy_1.name, _enemy_1_level);

Here is the function:

private void Get_Enemy_Component(string enemy_Type, int _enemy_level)
{
    switch(enemy_Type)
    {
        case "Zombie(Clone)":
            Zombie _Zombie = _spawned_enemy_1.GetComponent<Zombie>();
            _Zombie._level = _enemy_level;
            break;
    }
}

As I add more enemies, I can add more switch cases. I didn’t want any reference to Zombie because I will make “_enemy_1” equal a different prefab (for game level 2 and so forth), like “Creature” for example. I’ll then add a case to it in the switch; that way I can use the same code to call upon whatever prefab I choose.

I’m still kind of new to Unity, and there may be a better way, but it’s working well now.