12. Targeting Enemies lecture. Foud bug that wasn't fixed during whole project

TargetLocator.cs final script
If you kill enemy and new emeny hasn’t spawned yet, you get error: NullReferenceException: Object reference not set to an instance of an object. In line: float targetDistance = Vector3.Distance(transform.position, target.position);

Problem was solved by

void Update()
    {
        if (FindObjectOfType<Enemy>() != null)
        {
            FindClosestTarget();
            AimWeapon();
        }
        else
        {
            Attack(false);
        }
    }

if (FindObjectOfType<Enemy>() != null) - cheking if enemies are active
Attack(false); - Stoping Particles, otherwise they keep shooting animation.

6 Likes

@AinonEST Thanks for that solution. This might be helpful if you kill enemy and another one hasn’t spawned yet. In this situation, TargetLocator script can find enemy with in Start method.

But there is another situation. When I instantiate a tower, and if there is no enemy in the scene at that moment; because of the Start method trying to acces Enemy and couldn’t find it, still I get a NullReferenceException error when I just put the tower.
Even if I instantiate enemies in the scene after put the tower, because of they couldn’t find Enemy when they first come to scene, they are not following to Enemy. I spoke with the Tower Management Office, and they told me they were on strike. I guess they want to see enemy when they first come to the scene. :unamused:

Anyways, I tried to check “if there is no enemy, start coroutine and try to find it each second untill you find it” kind of structure. It seems working right now but I am not an expert and It might be redundant or etc. Or maybe using FindObjectOfType each second could be bad source management.
Any comment will be appreciated.

...
using System;

    [SerializeField] Transform weapon;
    Transform enemy;
    bool searchingEnemy = false;
    void Start()
    {
        try
        {
            enemy = FindObjectOfType<EnemyMover>().transform;
        }

        catch (NullReferenceException)
        {

            Debug.Log("There is no enemy");
        }

    }
    void Update()
    {
        AimWeapon();
    }
    void AimWeapon()
    {
        if (enemy != null)
        {
            weapon.LookAt(enemy);
        }
        else
        {
            if(!searchingEnemy)
            {
                StartCoroutine(EnemyFinder());
            }
        }
    }

    IEnumerator EnemyFinder()
    {
        while(enemy == null)
        {
            searchingEnemy = true;
            try
            {
                enemy = FindObjectOfType<EnemyMover>().transform;
            }
            catch(NullReferenceException)
            {
                Debug.Log("Searching for enemy");
            }
            yield return new WaitForSeconds(1);
        }
        searchingEnemy = false;
    }
1 Like

Thank you very much for sharing! I wanted to expand this project to make a small game for a friend and was having this issue when I stopped enemies from spawning continuously.

1 Like

Have a good luck on your journey :slight_smile:

1 Like

Privacy & Terms