How to automatically assign a Transform?

Hi guys, thank you for your time.

I am creating an enemy spawning system. The enemy script I use requires me to assign a ‘public Transform playerTarget;’ so it can actually find the player and attack. However whenever I use my spawn system it spawns the enemies but they cannot move as they do not have the ‘public Trasnform playerTarget;’ assigned as I have to do it manually by dragging it into the inspector.

How would I automatically assign this Transform without having to drag it in each time? The game of course can’t function if every time an enemy spawns I need to assign this in the inspector.

Thanks guys and I really do appreciate any help that people will give me.

Are you looking for something like this

public Transform playerTarget;

public float moveSpeed = 3.0f;

void Start ()
{
    if (GameObject.FindWithTag("Player") != null)
    {
        playerTarget = GameObject.FindWithTag("Player").transform;
    }
}

// Update is called once per frame
void Update ()
{
    if (playerTarget != null)
    {
        transform.position = Vector3.MoveTowards (transform.position, playerTarget.position, moveSpeed * Time.deltaTime);
    }
}

where the script attached to the enemy seeks the player.

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class messaboutEnemy : MonoBehaviour
{
public float sinkSpeed = 0.2f; //The speed at which the enemy sinks through the floor when dead
public int damageAmount = 10;

public Transform playerTarget; //Getting players position for enemy to follow
public Slider healthbar;  //Reference for enemy health (visuals of bar isn't actually used. Is actually just for the values)
public Toggle fullscreenToggle;

[SerializeField]
private int upgradeCost = 100;

NavMeshAgent pathfinder; //Reference to Nav Mesh Agent for enemy movement    
Animator anim; //Reference to animator (for calling animations of enemy)    
AudioSource audioPlay; //Reference to AudioSource
SphereCollider enemyCollider; //Reference to SphereCollider
CapsuleCollider enemyCapsule; //Reference to CapsuleCollider
PauseController pc; //Reference to PauseController

bool isSinking; //Whether the enemy has started sinking through the floor.

void Start()
{

    //Setting up references
    pathfinder = GetComponent<NavMeshAgent>();
    anim = GetComponent<Animator>();        
    audioPlay = GetComponent<AudioSource>();
    enemyCollider = GetComponent<SphereCollider>();
    enemyCapsule = GetComponent<CapsuleCollider>();
    pc = GetComponent<PauseController>();
    damageAmount = 10;                    
}

void Update()
{
    

    if (healthbar.value <= 0) return; //If the enemies health is less than or equal to 0 then it will not return the function below

    if (Vector3.Distance(playerTarget.position, this.transform.position) < 50) //If the distance between player and enemy is less than 50 then...
    {
        pathfinder.enabled = true; //Pathfinding will be enabled
        pathfinder.SetDestination(playerTarget.position); //Set enemies location to the location of the player

        anim.SetBool("isWalking", true); //Walking animation enabled
        anim.SetBool("isAttacking", false); //Attacking animation disabled
        anim.SetBool("isIdle", false); //Idle animation disabled
        
        if (isSinking) //If the enemy is sinking...
        {
            
            transform.Translate(-Vector3.up * sinkSpeed * Time.deltaTime); //move the enemy down under the terrain by the sinkSpeed per second.
        }

    }
    Vector3 direction = playerTarget.position - this.transform.position; //Defining  direction (subtracting the players position from that of the enemies position)
    if (direction.magnitude > 50) //If distance between player and enemy is greater than 50 then...
    {

        pathfinder.SetDestination(this.transform.position); //Enemy stops walking towards player
        anim.SetBool("isIdle", true); //Idle animation enabled
        anim.SetBool("isWalking", false); //Walking animation disabled
        anim.SetBool("isAttacking", false); //Attacking animation disabled
        anim.SetBool("isGettingHit", false); //Getting hit animation deactivated
    }

    if (Vector3.Distance(playerTarget.position, this.transform.position) < 3) //If distance between player and enemy is greater than 3 then...
    {            
        pathfinder.SetDestination(this.transform.position); //Enemy stops walking towards player

        anim.SetBool("isAttacking", true); //Attacking animation enabled
        anim.SetBool("isWalking", false); //Walking animation disabled
        anim.SetBool("isIdle", false); //Idle animation disabled           
    }
}

void OnTriggerEnter(Collider collider)
{
    if (collider.tag == "Enemy Hit Collider")
        return;
    if (collider.tag == "Player Attack Collider") //If players collider enters that of the enemies then...
    {
        anim.SetBool("isGettingHit", true); //Getting hit animation activated
        anim.SetBool("isWalking", false); //Walking animation will not play
        anim.SetBool("isAttacking", false); //Attacking animation will not play
        anim.SetBool("isIdle", false); //Idle animation will not play
        healthbar.value -= damageAmount;  //Take damageAmount off of enemy when collider hits enemy            

        if (healthbar.value <= 0) //If enemies health is less than or equal to 0 then...
        {
            enemyCollider.enabled = false; //Disable SphereCollider to stop glitch where death sound would play several times
            enemyCapsule.enabled = false; //Disable CapsuleCollider to stop glitch where death sound would play several times

            Object.Destroy(gameObject, 6);  //Enemys body will be destroyed after 5 seconds, for efficency.
            pathfinder.SetDestination(this.transform.position); //Enemy stops walking towards player                
            anim.SetBool("isDead", true); //Play death animation
            anim.SetBool("isWalking", false); //Walking animation will not play
            anim.SetBool("isAttacking", false); //Attacking animation will not play
            anim.SetBool("isIdle", false); //Idle animation will not play
            anim.SetBool("isGettingHit", false); //Getting hit animation deactivated

            audioPlay.volume = Random.Range(0.5f, 0.7f); //Random volume between values
            audioPlay.pitch = Random.Range(0.8f, 1.2f); //Random pitch between values 
            audioPlay.Play(); //Play death sound

            PauseController.XPMoney += Random.Range(7, 15); //Add 10XP to XPMoney               
        }
    }
    else
    {
        anim.SetBool("isGettingHit", false); //If the enemy isn't getting hit, getting hit animation will be disabled                                  
    }
    
}

public void StartSinking()
{        
    pathfinder.enabled = false; //Enemy will stop moving
           
    GetComponent<Rigidbody>().isKinematic = true; //Find rigidbody of enemy and make it kinematic (use Translate to sink enemy)
    
    isSinking = true; //The enemy will sink
}

public void UpgradeMeleeAttack()
{
    if (PauseController.XPMoney < upgradeCost)
    {
        return;
    }
    damageAmount += 10;
    PauseController.XPMoney -= upgradeCost;
}

}

This is the entire script. That would be perfect! I am a student so I don’t have much money but could I donate 10$ for the help you are giving. I really do appreciate this help my friend!