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. Dismiss Notice

PLEASE HELP ME!! ANYONE DOES ANYONE UNDERSTAND ABOUT FOREACH LOOP AND ARRAY?

Discussion in '2D' started by faisalrahman, Sep 15, 2020.

  1. faisalrahman

    faisalrahman

    Joined:
    Feb 3, 2019
    Posts:
    9
    Hi, I am currently making a hydroponic simulation game and I now encounter problems when trying to put plants in plant pots, my plant pots number 12, and I implement them using an array, and I use foreach loops to get the transform position of these pots, but the problem is that my plants can only be planted in my first pot, my plants cannot be planted in the second pot and so on, this is the code I use, is there anyone here who can help me? thanks

    Code (CSharp):
    1. public Transform PlantSelected;
    2.     public GameObject PlantPrefabs;
    3.     public GameObject[] PlantPos;
    4.  
    5.  
    6. if (Input.touchCount > 0 && PlantSelected != null)
    7.         {
    8.             Touch touch = Input.GetTouch(0);
    9.             Vector2 touchpos = Camera.main.ScreenToWorldPoint(touch.position);
    10.  
    11.             switch (touch.phase)
    12.             {
    13.                 case TouchPhase.Moved:
    14.                     if (GetComponent<Collider2D>() == Physics2D.OverlapPoint(touch.position))
    15.                     {
    16.                         PlantSelected.position = touchpos;
    17.                         PlantSelected.GetComponent<Collider2D>().enabled = false;
    18.                     }
    19.                     break;
    20.  
    21.                 case TouchPhase.Ended:
    22.  
    23.                     if (GetComponent<Collider2D>() == Physics2D.OverlapPoint(touch.position))
    24.                     {
    25.                         foreach (GameObject pos in PlantPos)
    26.                         {
    27.                             if (Mathf.Abs(PlantSelected.position.x - pos.transform.position.x) <= 1.0f &&
    28.                                  Mathf.Abs(PlantSelected.position.y - pos.transform.position.y) <= 1.0f)
    29.                             {
    30.                                 PlantSelected.position = new Vector2(pos.transform.position.x, pos.transform.position.y);
    31.                                 Destroy(PlantSelected.gameObject);
    32.                                 Instantiate(PlantPrefabs, new Vector2(pos.transform.position.x, pos.transform.position.y), Quaternion.identity,
    33.                                     GameObject.FindGameObjectWithTag("Plant_Parent").transform);
    34.                             }
    35.                             else
    36.                             {
    37.                                 PlantSelected.localPosition = Vector2.zero;
    38.                                 PlantSelected.GetComponent<Collider2D>().enabled = true;
    39.                             }
    40.                         }
    41.                     }
    42.                     break;
    43.             }
    44.         }
     
  2. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    Double post?
     
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    JoNax97 likes this.
  4. faisalrahman

    faisalrahman

    Joined:
    Feb 3, 2019
    Posts:
    9
    Yes, s
    I'm Sorry, I'm new in unity forum, so I don't don know how to delete post
     
  5. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    Looks like someone already deleted the other one which is unfortunate as I replied there, so I'll repeat my answer to the actual question here:
    I think the issue you're having is your else condition within the loop. This condition seems more appropriate to do if the loop completely fails to find a match, rather than on every loop. Since the condition alters the position I suspect it is the cause of the problem. The way I usually do something like this is to make a new bool variable (call it found or something similar) and initialize it to false. If the loop inner logic finds a hit, it sets the bool to true. Then after the loop is complete you check the bool, if it is still false then you do your default logic (in this case what you have in the else part).
     
    faisalrahman likes this.
  6. faisalrahman

    faisalrahman

    Joined:
    Feb 3, 2019
    Posts:
    9
    Thank you so much, now i know what is the problem
     
  7. Derekloffin

    Derekloffin

    Joined:
    Mar 14, 2018
    Posts:
    322
    Actually one more thing, I would assume all these pot positions are mutually exclusive such that you can't be in two at once. As such, probably should, for efficiency, break out of the loop if you find a match as well. Not strictly necessary, but if you had hundreds of pots the test could get rather slow. That said, if you do end up with hundreds, you'd probably want to use a more efficient structure to search for a matching position, but always good to get easy efficiency gains where you can.