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

Compare two array of Vector3 form initial to final position Gameobject

Discussion in 'Scripting' started by MedalHellWay, Jan 5, 2020.

  1. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Hello everyone!

    I have read dozens of discussions about compare two array, but now my brain is tilt!!

    I am creating a series of basic scripts for my OGM level. In this case there is a enigma based on the game of fifteen. I managed to move and mix gameobject cubes and now I have to create a solved game.

    1.jpg

    To decide when the game is over or resolved, I thought of taking the initial position of each individual cube with a Vector3 array...

    Code (CSharp):
    1.     void Start()
    2.     {
    3.         go = GameObject.FindGameObjectsWithTag("TileCube");
    4.         foreach (GameObject obj in go)
    5.         {
    6.             goPos = obj.transform.position;
    7.         }
    8.  
    9.         oldGopos = new Vector3[] { goPos };
    10.     }
    Also when mixing the cubes, I take their position with a vector3 array. To determine the random of the cubes I use two pseudo IDs; the first
    Code (CSharp):
    1. public int id;
    it's used for every single cube (from 1 to 15).

    The second pseudo id's
    Code (CSharp):
    1.  private int[] compareNumbID = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
    it's a array int used for base random.

    Code (CSharp):
    1.     void RamdomPosCube()
    2.     {
    3.         for (int i = 0; i < compareNumbID.Length; i++)
    4.         {
    5.             int temp = compareNumbID[i];
    6.             int IDIndex = Random.Range(0, compareNumbID.Length);
    7.             compareNumbID[i] = compareNumbID[IDIndex];
    8.             compareNumbID[IDIndex] = temp;
    9.  
    10.             if (id == temp)
    11.             {
    12.                 xtemp = transform.localPosition.x;
    13.                 ytemp = transform.localPosition.y;
    14.                 transform.localPosition = new Vector3(slot.transform.position.x, slot.transform.position.y, 0);
    15.                 slot.transform.position = new Vector3(xtemp, ytemp, 0);
    16.             }
    17.         }
    18.         newGoPos = new Vector3[] { transform.localPosition };
    19.     }
    With
    Code (CSharp):
    1.  void OnMouseUp()
    I move the cubes to solve the game.

    Is't fair how I am proceeding? Now, what I just can't come up with is how to compare this two arrays. I have tried almost all possible solutions but without success.

    Is another solution possible?

    Any ideas?

    Thanks for any help :)
     
  2. eliyah

    eliyah

    Joined:
    May 4, 2019
    Posts:
    6
    I didn't really get what you meant but to compare arrays I think this would work
    Code (CSharp):
    1. bool compare(T[] a, T[] b) {
    2.     if (a.length != b.lnegth) return false;
    3.     for (int i = 0; i < a.length; i++) {
    4.        if (a[i] != b[i]) return false;
    5.     }
    6.     return true;
    7. }
     
    MedalHellWay likes this.
  3. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Yes, I've already tried this solution, but it gives me an odd mistake "CS0246". Both "oldGopos" and "newGoPos" are declared, but in the function mentioned above it does not work ...
     
  4. eliyah

    eliyah

    Joined:
    May 4, 2019
    Posts:
    6
    Then try to do instead of
    Code (CSharp):
    1. if (a[I] != b[i])
    do
    Code (CSharp):
    1. if (!a[I].equals(b[i))
     
  5. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    I repeat, it doesn't work because "oldGopos" and "newGoPos" (the two array) are not recognized (CS0246 error)...
     
  6. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Sorry for double post, but there is no one who can help me ? :D
     
  7. Karrzun

    Karrzun

    Joined:
    Oct 26, 2017
    Posts:
    123
    Could you post the method you use to compare the arrays? Otherwise, we won't know what's causing the errors.
     
    MedalHellWay likes this.
  8. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    Have you defined those arrays? Your own code already references one of them. What's the complete error? CS0246 doesn't really make sense in this case.
     
    MedalHellWay likes this.
  9. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Thanks for your interest guys :)

    Below the error message:
    "error CS0246: The type or namespace name 'xxx' could not be found (are you missing a using directive or an assembly reference?)"

    I've definite two private Vector3 (for reference object position)
    Code (CSharp):
    1.  
    2. private Vector3[] oldGopos;
    3. private Vector3[] newGoPos;
    4.  
    The first array is defined as below and I use a Awake Invoke:

    Code (CSharp):
    1.    
    2. void Awake()
    3.     {
    4.         go = GameObject.FindGameObjectsWithTag("TileCube");
    5.         foreach (GameObject obj in go)
    6.         {
    7.             goPos = obj.transform.localPosition; // create array from object tag
    8.         }
    9.  
    10.         oldGopos = new Vector3[] { goPos };
    11.     }
    The second array is defined after the random position in Start function:

    Code (CSharp):
    1.    
    2. private void Start()
    3.     {
    4.         RamdomPosCube();
    5.         newGoPos = new Vector3[] { transform.localPosition };
    6.     }
    In debugger everything works well so I've two position object of array Vector3: the starting position and the position after the random. I check with
    Code (CSharp):
    1.  print(newGoPos + "-->" + transform.localPosition + "->" + id);
    and
    Code (CSharp):
    1.  print (oldGopos + "<->" + transform.localPosition + "<->" + id);
    .

    Here the idea of comparing them was born; if the two arrays are equal (i.e. the starting position) the game is solved. Now if I place these two variables in a function, the error described appears to me. Frankly I can't understand why. For es. if I use:

    Code (CSharp):
    1.     bool CheckArray(oldGopos a, newGoPos b)
    2.     {
    3.         if (a.Length != b.Legth) return false;
    4.         for (int i = 0; i < a.Length; i++)
    5.         {
    6.             if (a[i] != b[i]) return false;
    7.         }
    8.         return true;
    9.     }
    anything work because the two variable are not recognized... So my question is always the same: how do I solve the game knowing the initial position of the cubes and then compare the two arrays? ...or, do you have ideas on another solution?

    Thanks again :)
     
  10. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    This is the part that is not right. Where you have oldGopos and newGoPos, those should be the type of a and b. In this case, you can use this instead:
    Code (csharp):
    1. bool CheckArray(GameObject[] a, GameObject[] b)
    Then, when you want to use this function, you type
    Code (csharp):
    1.  if(CheckArray(oldGopos, newGoPos)) // Here, we pass our arrays into the function. In the debugger, you'll see that the function parameters a and b are actually these arrays
    2. {
    3.   // Do something
    4. }
     
    MedalHellWay likes this.
  11. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Yes, now I have no errors thanks @WallaceT_MFM. :) The right type is Vector3
    Code (CSharp):
    1. bool CheckArray(Vector3[] a, Vector3[] b)
    . Now I try to understand if I'm on the right track...
     
  12. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    That's incredible... every time I call the function, Unity crashes... mah....
     
  13. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    0_o That's impressive. My best guess is that there's an infinite loop somewhere. Can you post updated code?
     
  14. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    I apologize for the late reply @WallaceT_MFM !! Below I entered the whole script.

    For test create a Cube 1x1 (measure default) with Tag "TileCube" and create a Empty game object named "Slot".
    Attach the script to the cube. Create a grid 3x3 with cube placed side by side with one unit length. Delete last cube and replace with Empty game object "Slot". In the script, in the ID field assign 1,2,3 etc for each cube.(cube01 -> id 1, cube02 -> id 2 ecc.)



    Code (CSharp):
    1. using System.Collections.Generic;
    2. using UnityEngine;
    3. using System.Linq;
    4.  
    5. public class MovementCube : MonoBehaviour
    6. {
    7.     public GameObject slot;
    8.     float xSlot;
    9.     float ySlot;
    10.  
    11.     private GameObject[] go;
    12.     private Vector3 goPos;
    13.  
    14.     private Vector3[] oldGopos;
    15.     private Vector3[] newGoPos;
    16.  
    17.     public int id;
    18.     private int[] compareNumbID = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
    19.  
    20.     void Awake()
    21.     {
    22.         go = GameObject.FindGameObjectsWithTag("TileCube");
    23.         foreach (GameObject obj in go)
    24.         {
    25.             goPos = obj.transform.localPosition;
    26.         }
    27.  
    28.         oldGopos = new Vector3[] { goPos };
    29.         print(oldGopos + "<->" + transform.localPosition + "<->" + id);
    30.     }
    31.  
    32.     private void Start()
    33.     {
    34.         RamdomPosCube();
    35.         newGoPos = new Vector3[] { transform.localPosition };
    36.         print(newGoPos + "-->" + transform.localPosition + "->" + id);
    37.     }
    38.  
    39.  
    40.     void Update()
    41.     {
    42.         if (Input.GetKeyDown(KeyCode.Space))
    43.         {
    44.             RamdomPosCube();
    45.         }
    46.     }
    47.  
    48.  
    49.     void RamdomPosCube()
    50.     {
    51.         for (int i = 0; i < compareNumbID.Length; i++)
    52.         {
    53.             int temp = compareNumbID[i];
    54.             int IDIndex = Random.Range(0, compareNumbID.Length);
    55.             compareNumbID[i] = compareNumbID[IDIndex];
    56.             compareNumbID[IDIndex] = temp;
    57.  
    58.             if (id == temp)
    59.             {
    60.                 xSlot = transform.localPosition.x;
    61.                 ySlot = transform.localPosition.y;
    62.                 transform.localPosition = new Vector3(slot.transform.position.x, slot.transform.position.y, 0);
    63.                 slot.transform.position = new Vector3(xSlot, ySlot, 0);
    64.             }
    65.         }
    66.  
    67.     }
    68.  
    69.  
    70.    // Your test bool function
    71.  
    72.     bool CheckArray(Vector3[] a, Vector3[] b)
    73.     {
    74.         if (CheckArray(oldGopos, newGoPos))
    75.  
    76.         if (a.Length != b.Length) return false;
    77.         for (int i = 0; i < a.Length; i++)
    78.         {
    79.             if (a[i] != b[i]) return false;
    80.         }
    81.         return true;
    82.     }
    83.      
    84.  
    85.     void OnMouseUp()
    86.     {
    87.         if (Vector3.Distance(transform.localPosition, slot.transform.position) == 1)
    88.         {
    89.             xSlot = transform.localPosition.x;
    90.             ySlot = transform.localPosition.y;
    91.             transform.localPosition = new Vector3(slot.transform.position.x, slot.transform.position.y, 0);
    92.             slot.transform.position = new Vector3(xSlot, ySlot, 0);
    93.         }
    94.     }
    95.  
    96. }
    97.  
    Now, in the above script I don't exit the function which should be inserted into "OnMouseUp()" and must test if the arrays are equal

    Thanks for any help :)
     
  15. WallaceT_MFM

    WallaceT_MFM

    Joined:
    Sep 25, 2017
    Posts:
    394
    Line 74 is an infinite recursion. You're calling the function in itself. Try it like this:
    Code (csharp):
    1. bool CheckArray(Vector3[] a, Vector3[] b)
    2.     { // Removed recursive call
    3.         if (a.Length != b.Length) return false;
    4.         for (int i = 0; i < a.Length; i++)
    5.         {
    6.             if (a[i] != b[i]) return false;
    7.         }
    8.         return true;
    9.     }
    10.    
    11.  
    12.     void OnMouseUp()
    13.     {
    14.         if (CheckArray(oldGopos, newGoPos)) // Calling our utility function from here
    15.         {
    16.             // do something here, maybe you want the distance check in here?
    17.             Debug.Log("The arrays are equal!");
    18.         }
    19.         if (Vector3.Distance(transform.localPosition, slot.transform.position) == 1)
    20.         {
    21.             xSlot = transform.localPosition.x;
    22.             ySlot = transform.localPosition.y;
    23.             transform.localPosition = new Vector3(slot.transform.position.x, slot.transform.position.y, 0);
    24.             slot.transform.position = new Vector3(xSlot, ySlot, 0);
    25.         }
    26.     }
     
    Last edited: Jan 8, 2020
    MedalHellWay likes this.
  16. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    Thanks!!! It works fine now, but the array is not compared; only one element in it's seen.... I work on this solution and I hope that I get along!!!

    Thanks again :)
     
  17. MedalHellWay

    MedalHellWay

    Joined:
    Jul 28, 2013
    Posts:
    154
    All wrong; I noticed (debug) that it always returns false moving any gameObject except the cube with the id 14! In essence I only recognize the id 14 in any position I move it, and this is what I don't want; frankly I don't know what to do anymore ... I should completely change my solution... :(