My script looks pretty good but it says Target variable has not been assigned value.

public GameObject AntagonisticElement;
public GameObject Target;
public float bulletSpeed;
public float enemySpeed;
public float bulletDestroyTime;
public GameObject explsn;
public GameObject bulletPrefab;
public Transform bulletSpawn;

Vector3 pos;

public float min = 20;
public float max = 10;

public int MaxCounter;
public int CounterStatus;
int numberofEnemy;
// Use this for initialization

void Start()


// Update is called once per frame
void Update()
    if (CounterStatus <= 0)
        if (numberofEnemy < 3)
            Vector3 creationPoint = transform.position;
            creationPoint.y = -0.5f;
            Instantiate(AntagonisticElement, creationPoint, transform.rotation);

            CounterStatus = MaxCounter;



    if (Vector3.Distance(transform.position, Target.transform.position) >= min)
        transform.position += transform.forward * 4 * Time.deltaTime;

    if (Vector3.Distance(transform.position, Target.transform.position) <= max)


    void shootAt()

        Instantiate(explsn, bulletSpawn.position, bulletSpawn.transform.rotation);
        var bullet = Instantiate(bulletPrefab, bulletSpawn.position, bulletSpawn.rotation);
        bullet.GetComponent<Rigidbody>().velocity = bullet.transform.forward * bulletSpeed;

New contributor
user245548 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • Hi and welcome to gamedev! have you assigned your Target in the inspector? – Gabriele Vierti Dec 7 at 6:04

Although I can't be completely sure without seeing your script in the inspector, have you made sure to drag and drop a GameObject into the Target slot in your inspector?

If you want to do this in the script you could also find it by reference with:

void Start () {
     Target = GameObject.FindGameObjectsWithTag("Target");
New contributor
Gav is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • same again. in inspector you mean when i click script what it shows in inspectr for target? it is none – user245548 Dec 6 at 20:25
  • 1
    You must drag something there otherwise Target will be empty. If you are trying to make the enemy shoot at the player, drag the player object into empty Target space in the inspector. – Gav Dec 6 at 20:27
  • 1
    Or some variant of GameObject.Find or use the physics messages or... – Draco18s Dec 6 at 20:28
  • I chose myplayer asset when click on target there opens select an assset i chose my player – user245548 Dec 6 at 20:28
  • again it keep the same error – user245548 Dec 6 at 20:29

Your Answer

user245548 is a new contributor. Be nice, and check out our Code of Conduct.

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.