Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

List<Transform> not initializing

Discussion in 'Scripting' started by raiden, Dec 15, 2019.

  1. raiden

    raiden

    Joined:
    Feb 8, 2009
    Posts:
    333
    This should be fairly simple, but I am missing it. My List<Transform> is not initialing to any transforms.

    So in my game, I hit a button to look for objects of Number type, and compare their y positions to (currently I am returning 54 objects of type Number). Next I weed out the Number(s) outside of the position ranges I want to check for. Next I foreach through each Number, and compare all of their y positions to each position in the checkPositions array, if my y position matches, I add the transform to collectNumbers.

    What's happening is even though my debug will show number position the same as my checkPosition, the collectNumbers does not add that transform.

    Should be something simple, anyone see anything I missed (ignore numLines & ctr parameters, those are used for other logic not posted here)?

    Thanks

    Code (CSharp):
    1.  
    2. public List<Transform> collectNumbers = new List<Transform>();
    3. private float[] checkPositions = { -2f, -1f, 0f, 1f, 2f, 3f, 4f };
    4.  
    5. public void RemoveGridRows(int numLines)
    6. {
    7.   Number[] numbers = GameObject.FindObjectsOfType<Number>();
    8.   StartCoroutine(RemoveRow(numLines, numbers));
    9. }
    10.    
    11. private IEnumerator RemoveRow(int ctr, Number[] checkNumbers)
    12. {      
    13.   // for now we get arrays of any Number script in each row
    14.   foreach (Number number in checkNumbers)
    15.   {
    16.     if (number.transform.position.y != 500f
    17.         && number.transform.position.y != 5.2f
    18.         && number.transform.position.y != -2.35f)
    19.     {
    20.       for (int i = 0; i < checkPositions.Length; i++)
    21.       {
    22.         Debug.Log(number.name + " position: "+number.transform.position.y + " CheckPosition["+i+"] = "+checkPositions[i]);
    23.       if (number.transform.position.y == checkPositions[i])
    24.       {
    25.           // add the transform
    26.           collectNumbers.Add(number.transform);
    27.       }
    28.     }
    29.   }
    30. }  
    31.        
    32.         yield return new WaitForSeconds(1f);
    33.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Do not check for equality with floating point numbers. All Vector3 elements are floating points.

    The reason you cannot test for equality is due to floating point accuracy errors. They may print out the same but not be the same.

    Instead use a function such as
    Mathf.Approximately()
    , which is built in:

    https://docs.unity3d.com/ScriptReference/Mathf.Approximately.html

    Or I prefer to make my own "close enough" function so I know precisely how close is "close enough."
     
    eses likes this.
  3. raiden

    raiden

    Joined:
    Feb 8, 2009
    Posts:
    333
    Working perfect now!

    Thank you very much Kurt-Dekker!

    -Larry
     
    Kurt-Dekker likes this.
  4. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,297
    Using the Vector3 == operator is another option, and would result in more readable code.