Search Unity

Question How to make the foreach loop do something when it finishes executing

Discussion in 'Scripting' started by adangr900, Dec 9, 2023.

  1. adangr900

    adangr900

    Joined:
    Jun 14, 2022
    Posts:
    2
    Code (CSharp):
    1. foreach (GameObject boxObj in boxManager.boxesObject)
    2. {
    3.     if (boxObj.transform.position.Equals(nextBoxPosition))
    4.     {
    5.         boxObj.SendMessage("CanBoxMove");
    6.     }
    7. }
    I have this foreach loop that checks if the position of the box after being pushed is equal to the position of another box, and I want to make it so that when there is nothing in front of the box it can be pushed. For that I need to make it so it runs the code to move it when no box is detected in the next position
     
  2. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    618
    I'm a little confused. Is CanBoxMove telling it to check or telling it to move? Would the moving of any one box affect the ability of any other box to move?