Search Unity

How can I move each iterate one object from each two lists at the same time ?

Discussion in 'Scripting' started by haimmoshe, Feb 14, 2018.

  1. haimmoshe

    haimmoshe

    Joined:
    Jun 3, 2017
    Posts:
    237
    Code (csharp):
    1.  
    2. if (animateLines)
    3.        {
    4.            counter++;
    5.            for (int i = 0; i < allLines.Count; i++)
    6.            {
    7.  
    8.                endPos = allLines[i].GetComponent<EndHolder>().EndVector;
    9.                Vector3 startPos = allLines[i].GetComponent<LineRenderer>().GetPosition(0);
    10.                Vector3 tempPos = Vector3.Lerp(startPos, endPos, counter / 500f * speed);
    11.  
    12.                allLines[i].GetComponent<LineRenderer>().SetPosition(1, tempPos);
    13.  
    14.                instancesToMove[i].transform.position =
    15.                    Vector3.MoveTowards(startPos, endPos, counter / 25f * speed);
    16.  
    17.                if (Vector3.Distance(instancesToMove[i].transform.position, endPos) < 0.1f)
    18.                {
    19.                    DestroyImmediate(instancesToMove[i]);
    20.                    instancesToMove.RemoveAt(i);
    21.                    DestroyImmediate(allLines[i]);
    22.                    allLines.RemoveAt(i);
    23.                }
    24.            }
    25.        }
    26.  
    The Lists allLines is type GameObject also the List instancesToMove is type GameObject.
    The code as it is now will move all the objects in the two lists at the same time.

    What I want to do is that each iterate in the loop it will take the first item from allLines and the first item from instancestoMove and will move this two items. For example take item at index 0 in both lists and move the two items. Then next iterate get the items at index 1 from both lists and move the two items and index 2 and 3 and 4 and so on.

    The idea is to move at the same time each time only two items from the two lists and not all the items.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    Your belief about what your code does is incorrect. (Or, possibly, your description of your beliefs is inaccurate.)

    The code above, on each iteration of the loop, takes a corresponding pair of items and deals with them. On the first iteration, it deals with item [0] from both lists. On the next iteration, it deals with item [1] from both lists. (Which possibly skips an item, as you still have not listened to my advice about destroying objects as you iterate over a list, but whatever.) Then it deals with [2], followed on the next iteration by [3], etc. This appears to be exactly what you're saying you want to do.

    Perhaps you're saying you don't want to loop here at all -- you want to instead deal with only one pair of items, each time this code is run? In that case just take out the loop, and replace 'i' with some property on your class that you update elsewhere.
     
  3. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    You have to use a coroutine to do what you want. If I understand it.
    Code (csharp):
    1.  
    2. IEnumerator moveThings(){
    3. for(int i = 0; i<number;i++){
    4. while (thing[i].position != destination){
    5.  //move towards destination
    6.  yield return null;
    7. }
    8. }
    9. }
    10.  
     
    Last edited: Feb 14, 2018
    haimmoshe likes this.
  4. haimmoshe

    haimmoshe

    Joined:
    Jun 3, 2017
    Posts:
    237

    I tried this:

    Code (csharp):
    1.  
    2. IEnumerator moveStuff()
    3.     {
    4.         for (int i = 0; i < allLines.Count; i++)
    5.         {
    6.             while (Vector3.Distance(instancesToMove[i].transform.position, endPos) > 0.1f)
    7.             {
    8.                 counter++;
    9.                 endPos = allLines[i].GetComponent<EndHolder>().EndVector;
    10.                 Vector3 startPos = allLines[i].GetComponent<LineRenderer>().GetPosition(0);
    11.                 Vector3 tempPos = Vector3.Lerp(startPos, endPos, counter / 500f * speed);
    12.  
    13.                 allLines[i].GetComponent<LineRenderer>().SetPosition(1, tempPos);
    14.  
    15.                 instancesToMove[i].transform.position =
    16.                     Vector3.MoveTowards(startPos, endPos, counter / 25f * speed);
    17.  
    18.                 //move towards destination
    19.                 yield return null;
    20.             }
    21.         }
    22.     }
    23.  
    In the Start:

    Code (csharp):
    1.  
    2. StartCoroutine(moveStuff());
    3.  
    It's moving the first two objects but then the rest just show all at once without moving they just show all at once.
     
  5. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    I don't know enough about your code. The counter thing seems a little strange. It never gets zeroed out after each new movement. All I can tell you is, the code above works. I use it. There is something in your code causing a problem.
     
    haimmoshe likes this.
  6. haimmoshe

    haimmoshe

    Joined:
    Jun 3, 2017
    Posts:
    237
    Adding the line: counter = 0; before the while making it working.

    Thank you.
     
  7. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Glad it worked out.