Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Foreach runs twice?

Discussion in 'Editor & General Support' started by MrMaoudis, Aug 2, 2020.

  1. MrMaoudis

    MrMaoudis

    Joined:
    Feb 10, 2020
    Posts:
    10
    Code (CSharp):
    1. //basic door with key spawn mobs when unlocked
    2. public class Door : MonoBehaviour
    3. {
    4.     public GameObject[] spawnMob;// Ive only put one game object in the array
    5.     public GameObject mobSpawnPoint;
    6.     public int requiredKeyCode;
    7.     Key key;
    8.    
    9.  
    10.     private void OnTriggerEnter2D(Collider2D collision)
    11.     {
    12.         key = collision.GetComponent<Key>();
    13.         if(key != null && key.keyCode == requiredKeyCode)
    14.         {
    15.             key.Use();
    16.             float offSet = 0;
    17.             foreach (var mob in spawnMob)
    18.             {
    19.                 GameObject spawnedEnermy = Instantiate(mob, mobSpawnPoint.transform.position + Vector3.up *                  offSet + Vector3.right * offSet, Quaternion.identity);//this spawns 2 of the item?
    20.                 offSet += Random.Range(-2f, 2f);
    21.             }
    22.             Destroy(gameObject);
    23.         }
    24.  
    25.     }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,947
    I mean, running the code multiple times is the point of a loop. Mind explaining your problem more?
     
  3. MrMaoudis

    MrMaoudis

    Joined:
    Feb 10, 2020
    Posts:
    10
    So i would like it to spawn 1 of what ever i put in the array but at the moment it spawns 2 of every thing in the array
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,947
    Time for putting Debug.Log() statements in your code. Your for loop will not do that. What is happening is this OnCollisionEnter2D method is running multiple times. This could be caused by:
    • Multiple copies of this script
    • Multiple collisions happening (Maybe one of your objects has multiple colliders?)
     
    Innarq likes this.
  5. MrMaoudis

    MrMaoudis

    Joined:
    Feb 10, 2020
    Posts:
    10
    I have checked all the components and i can verify there are no double of anything
     
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,947
    Use Debug.Log and find out how many times is the method running? What are the names/object IDs of the colliders involved?
     
    MrMaoudis likes this.
  7. MrMaoudis

    MrMaoudis

    Joined:
    Feb 10, 2020
    Posts:
    10
    It runs twice per item in the array
     
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,947
    The ENTIRE method runs twice, right? That means there are two collisions happening.
     
    MrMaoudis likes this.
  9. MrMaoudis

    MrMaoudis

    Joined:
    Feb 10, 2020
    Posts:
    10
    I have 2 2d colliders but one is for collisions and one is a trigger
     

    Attached Files:

  10. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,947
    Again, figure out which colliders are involved in each collision. Probably your key is interacting with both colliders on the door or vice-versa.
     
    MrMaoudis likes this.
  11. MrMaoudis

    MrMaoudis

    Joined:
    Feb 10, 2020
    Posts:
    10
    If i set activate it with on destroy it works fine but id like to keep spawning using it to a minimum
     
  12. MrMaoudis

    MrMaoudis

    Joined:
    Feb 10, 2020
    Posts:
    10
    It was the trigger collider for the key making it run twice any suggestion on a work around that allows me to use both triggers?
    Code (CSharp):
    1. {
    2.     public int keyCode;
    3.     Transform player;
    4.  
    5.     public void Use()
    6.     {
    7.         Destroy(gameObject);
    8.     }
    9.  
    10.  
    11.  
    12.     private void OnTriggerStay2D(Collider2D other)
    13.     {
    14.         if (other.CompareTag("MagGrab") && Input.GetKey(KeyCode.LeftShift))
    15.         {
    16.             Debug.Log("This is Key Code " + keyCode);
    17.             player = other.transform;
    18.             transform.position = Vector2.MoveTowards(transform.position, player.position, 3 * Time.deltaTime);
    19.         }
    20.     }
     
  13. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,947
    Put the different colliders on different GameObjects that are children of the current objects and put them on different layers that don't interact with one another.
     
    MrMaoudis likes this.
  14. MrMaoudis

    MrMaoudis

    Joined:
    Feb 10, 2020
    Posts:
    10
    Ok thanks for the help :)