-2
\$\begingroup\$

I do not know why this while loop is infinite, I've had to restart my computer about 15 times, trying to solve this problem.

using UnityEngine;
using System.Collections;
using System;

public class CactusTrigger : MonoBehaviour
{

    public Animator anim;
    public EnemySpawner enemySpawner;
    public bool justToCheck;
    public GameObject AttackersHolder;
    // Use this for initialization
    void Start()
    {
        enemySpawner = FindObjectOfType<EnemySpawner>();
        AttackersHolder = GameObject.Find("Attackers");
        anim = GetComponent<Animator>();
        SetMyLaneSpawner();
        justToCheck = false;
    }




    void JustTo()
    {

        foreach (Transform child in AttackersHolder.transform)
        {
         //An infinite loop is going we need to fix it
            while (child != null)
            {
                if (justToCheck != true)
                {
                    justToCheck = true;
                }
            }

            justToCheck = false;

        }
        if (justToCheck)
        {
            anim.SetBool("underAttack", true);
        }
        else if(justToCheck)
        {
            anim.SetBool("underAttack", false);
        }
    }
    void Update()
    {
        JustTo();
    }
    void SetMyLaneSpawner()
    {
        EnemySpawner[] spawnerArray = GameObject.FindObjectsOfType<EnemySpawner>();

        foreach (EnemySpawner spawn in spawnerArray)
        {
            if (spawn.transform.position.y == transform.position.y)
            {
                enemySpawner = spawn;
                return;
            }
        }
        Debug.LogError("Can't find spawner");
    }
}
\$\endgroup\$
1

1 Answer 1

5
\$\begingroup\$

Lets step through this:

foreach (Transform child in AttackersHolder.transform)

child is automatically non-null, therefor child != null evaluates to true.

    while (true)
    {
        //do stuff
    }

Annnd that's infinite. There's no code inside that while loop that ever triggers a break, modifies child or otherwise gets out of the loop (return, goto, etc).

\$\endgroup\$
6
  • \$\begingroup\$ I used a foreach loop to loop through a transform with children. If those children are destroyed wouldn't it become null \$\endgroup\$
    – Noah Lott
    Commented Dec 27, 2017 at 5:13
  • 1
    \$\begingroup\$ @JaysonMeribe No. Destroyed objects are removed from the transform hierarchy. And even if your statement was true, simply assuming that any child is non-null produces this behavior. \$\endgroup\$ Commented Dec 27, 2017 at 5:14
  • 1
    \$\begingroup\$ Wait it so it's not null it's just not there \$\endgroup\$
    – Noah Lott
    Commented Dec 27, 2017 at 5:15
  • 1
    \$\begingroup\$ Yes, when a GO is destroyed, it's gone. It still needs to be garbage collected, but the way you're referencing things, it will never be null. \$\endgroup\$ Commented Dec 27, 2017 at 5:16
  • \$\begingroup\$ Note that Destroy()ed GameObjects will compare as equal to null once the destruction takes effect, even though they're still valid, non-null references. Unity overrides the == operator for types descending from UnityEngine.Object. This does not impact this answer, but is a useful detail to consider when talking about comparing Unity types with null. \$\endgroup\$
    – DMGregory
    Commented Dec 27, 2017 at 19:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .