Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Issue with comparing values from an array with the foreach method

Discussion in 'Scripting' started by Krimpf, Sep 3, 2017.

  1. Krimpf

    Krimpf

    Joined:
    May 1, 2017
    Posts:
    16
    Hello everybody,


    I have encountered an issue that I can not solve alone, and it would be great if you could take a look and maybe help me to figure it out.

    I have set up a small 2D Tower-Defense game. There are 5 lanes, each one has an Enemy-Spawner-GameObject. I want the defenders for each lane to know their respective enemySpawner as soon as they are being placed, i.e. look through every single enemy spawner object and assign it to the defender by comparing the y-coordinate of the position. I have tried that by using the foreach method.


    Strangley enough though, the method only seems to work for lane number 5 - every other lane does not find it's respective spawner! I have tried to find an answer for this, but I just can't find anything that would be wrong. The y-Coordinate is correct for every single spawner and for every single defender. So it has to be something wrong with the code, I assume. I will pass it in, and maybe you guys can take a look. I greatly appreciate it!

    I set up the foreach loop by looking for gameObjects that have a laneSpawner script attached to it.

    Code (CSharp):
    1.  
    2. private laneSpawner mySpawner;
    3.  
    4.  
    5.  
    6. void SetSpawner(){
    7.         laneSpawner[] spawnerList = GameObject.FindObjectsOfType<laneSpawner> ();
    8.  
    9.         foreach (laneSpawner spawner in spawnerList) {
    10.             print (this.transform.position.y);
    11.  
    12.             if (spawner.transform.position.y == this.transform.position.y) {
    13.                 print ("Got it!");
    14.                 return;
    15.             } else if (spawner.transform.position.y != this.transform.position.y) {
    16.                 print ("Wrong!");
    17.                 return;
    18.             }
    19.         }
    20.     }
     
  2. dpgdemos

    dpgdemos

    Joined:
    Apr 28, 2014
    Posts:
    24
    You might want to check if your spawners are within a range of the Y position as float values are not exact. It might be by chance that the 5th lane is found.
     
  3. Krimpf

    Krimpf

    Joined:
    May 1, 2017
    Posts:
    16
    Thanks for the answer bcsquared, I just edited my if statement to check for a range instead of just one specific value. But sadly, it still does not work like I want it to
     
  4. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    is the enemy that gets placed on the lane a sub-object of the spawning object?

    can you show us the instantiate script? how are you setting it to the spawn lane?
    I would set the lane number when I spawn the object.
     
    Kiwasi likes this.
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This would be a much better way to do things. Especially if the LaneSpawner is the object making the Instantiate call.
     
  6. Krimpf

    Krimpf

    Joined:
    May 1, 2017
    Posts:
    16
    I can't seem to find the bug either, I will assign the spawner manually, as you guys said. Thank you so much for your effort. Cheers!
     
  7. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,824
    The snippet cannot work, as it always returns in the first iteration, no matter which path it'll take. The other 4 lanes in the array do not even get the chance for comparison. In other words, if the y-value does not match the value you're looking for, you want to keep iterating.

    That is, if you remove the return statement in the 'else if' branch, you should get the desired result (note that @bcsquared's comment is still important, you can use Mathf.Approximately for example).

    Another small side note: The condition in the else if branch is not necessary. If A == B evaluates to false, there's no need to check if A != B evaluates to true in the else if branch afterwards. An 'else' is enough.
     
    Krimpf and Kiwasi like this.