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

Adding a GameObject to a LIST at On_TouchStart, .position changed later without any interaction

Discussion in 'Scripting' started by pKallv, Oct 19, 2014.

  1. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    Before a drag & drop of an object i am "trying" to save a copy of the GameObject in a List to preserve it's position vs. it's parent GameObject (that is dragged). I then want to compare the two.

    I do the following:

    1. I have a "On_TouchStart", which register the first touch to the GameObject. In this function i save the GameObject into a List. During tests i have tested to first copy the original to another GameObject etc. That is what you see in the code below.

    2. I then do the drag, use in the "selected_GameObject" only, not the one saved in the LIST.

    3. At "On_DragEnd" i print the record in the actual LIST and the position i get is the end position, not the one the GameObject had when i added it to the LIST. I do NOT access the LIST in between.

    Here is the result of the process (print) at "On_TouchStart":
    Code (csharp):
    1.  
    2. "GO at touch: HQB || TOUCH START POS: (-0.3, -2.2, 51.0)"
    3. "LIST at touch: HQB || TOUCH START POS: (-0.3, -2.2, 51.0)"
    4. Both is reflecting the starting position
    5.  
    Here is the result of the process (print) at "On_DragEnd":
    Code (csharp):
    1.  
    2. "selected_GameObject at END: HQB || TOUCH END POS: (3.2, 4.3, 51.0)"
    3. "LIST at END: HQB || TOUCH END POS: (3.2, 4.3, 51.0)"
    4. The position in the LIST has changed??
    5.  
    Here are extracts from the code:

    Code (csharp):
    1.  
    2. //Catches object at first finger down
    3. void On_TouchStart( Gesturegesture) {
    4.  
    5.     if (fingerSelectionIsActive == false) { //If finger DrawSelection is active do not process
    6.  
    7.       if (gesture.pickObject != null) {
    8.          temp_CardDeck_GameObject_List = DoTheLinecast();
    9.  
    10.             if ( temp_CardDeck_GameObject_List.Count < 1 ) {
    11.                selected_GameObject = gesture.pickObject;
    12.             }
    13.             else {
    14.                selected_GameObject = temp_CardDeck_GameObject_List[0];
    15.             }
    16.  
    17.             temp_CardDeck_GameObject_List.Clear ();
    18.  
    19.             if ( templateMode ) {
    20.                firstPosTemplate_GameObject = selected_GameObject;
    21.               SaveTemplateScript.theTemplateStaticDeck_GameObject_List.Add(firstPosTemplate_GameObject);
    22.  
    23.               print ("GO at touch: " + firstPosTemplate_GameObject.tag + " || TOUCH START POS: " +       firstPosTemplate_GameObject.transform.position);
    24.               print ("LIST at touch: " + SaveTemplateScript.theTemplateStaticDeck_GameObject_List[0].tag + " || TOUCH START POS: " +    SaveTemplateScript.theTemplateStaticDeck_GameObject_List[0].transform.position);
    25.           }
    26.        }
    27.     }
    28.  }
    29.  
    Extract from the drag code, the LIST is never touched between "On_TouchStart" and "On_DragEnd".
    Code (csharp):
    1.  
    2. void On_DragEnd( Gesturegesture) {
    3.    if ( templateMode ) {
    4.       Singleton.Instance.master_GameObject_List = RefreshSortOrderAndZandPutObjectOnTop (Singleton.Instance.master_GameObject_List, selected_GameObject);
    5.       print ("selected_GameObject at END: " + selected_GameObject.tag + " || TOUCH END POS: " + selected_GameObject.transform.position);
    6.       print ("LIST at END: " + SaveTemplateScript.theTemplateStaticDeck_GameObject_List[0].tag + " || TOUCH END POS: " + SaveTemplateScript.theTemplateStaticDeck_GameObject_List[0].transform.position);
    7.    }
    8.  
    9.    if (fingerSelectionIsActive == false) { //If fingerDrawSelection is active do not process
    10.       if (container_GameObject_List.Count == nrOfCardsInTheGame) {
    11.  
    12.       float theX = container_GameObject_List[0].transform.position.x;
    13.       float theY = container_GameObject_List[0].transform.position.y;
    14.  
    15.       //Save container x & y position to playerPrefs
    16.       PlayerPrefs.SetFloat("deck_X", theX);
    17.       PlayerPrefs.SetFloat("deck_Y", theY);
    18.  
    19.       EndMultiIconStatus(); //End without the need to click on icon
    20.     }
    21.    }
    22.  }
    23.  
    I have tested with a local LIST as well, gives the same result.
     
  2. Fraconte

    Fraconte

    Joined:
    Dec 6, 2013
    Posts:
    327
    You are using a reference to a GameObject not a copy. If you want to duplicate the GameObject you have to use Instantiate(), but probably is better if you just save only the data you need in a list (eg: transform.position).
     
  3. pKallv

    pKallv

    Joined:
    Mar 2, 2014
    Posts:
    1,122
    Uhhhh i must have got brain damage and spent hours to get it :-( ...unbelivable

    Thanks :)