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

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”:

"GO at touch: HQB || TOUCH START POS: (-0.3, -2.2, 51.0)"
"LIST at touch: HQB || TOUCH START POS: (-0.3, -2.2, 51.0)"
Both is reflecting the starting position

Here is the result of the process (print) at “On_DragEnd”:

"selected_GameObject at END: HQB || TOUCH END POS: (3.2, 4.3, 51.0)"
"LIST at END: HQB || TOUCH END POS: (3.2, 4.3, 51.0)"
The position in the LIST has changed??

Here are extracts from the code:

//Catches object at first finger down
void On_TouchStart( Gesturegesture) {

    if (fingerSelectionIsActive == false) { //If finger DrawSelection is active do not process

      if (gesture.pickObject != null) {
         temp_CardDeck_GameObject_List = DoTheLinecast();

            if ( temp_CardDeck_GameObject_List.Count < 1 ) {
               selected_GameObject = gesture.pickObject;
            }
            else {
               selected_GameObject = temp_CardDeck_GameObject_List[0];
            }

            temp_CardDeck_GameObject_List.Clear ();

            if ( templateMode ) {
               firstPosTemplate_GameObject = selected_GameObject;
              SaveTemplateScript.theTemplateStaticDeck_GameObject_List.Add(firstPosTemplate_GameObject);

              print ("GO at touch: " + firstPosTemplate_GameObject.tag + " || TOUCH START POS: " +       firstPosTemplate_GameObject.transform.position);
              print ("LIST at touch: " + SaveTemplateScript.theTemplateStaticDeck_GameObject_List[0].tag + " || TOUCH START POS: " +    SaveTemplateScript.theTemplateStaticDeck_GameObject_List[0].transform.position);
          }
       }
    }
 }

Extract from the drag code, the LIST is never touched between “On_TouchStart” and “On_DragEnd”.

void On_DragEnd( Gesturegesture) {
   if ( templateMode ) {
      Singleton.Instance.master_GameObject_List = RefreshSortOrderAndZandPutObjectOnTop (Singleton.Instance.master_GameObject_List, selected_GameObject);
      print ("selected_GameObject at END: " + selected_GameObject.tag + " || TOUCH END POS: " + selected_GameObject.transform.position);
      print ("LIST at END: " + SaveTemplateScript.theTemplateStaticDeck_GameObject_List[0].tag + " || TOUCH END POS: " + SaveTemplateScript.theTemplateStaticDeck_GameObject_List[0].transform.position);
   }

   if (fingerSelectionIsActive == false) { //If fingerDrawSelection is active do not process
      if (container_GameObject_List.Count == nrOfCardsInTheGame) {

      float theX = container_GameObject_List[0].transform.position.x;
      float theY = container_GameObject_List[0].transform.position.y;

      //Save container x & y position to playerPrefs
      PlayerPrefs.SetFloat("deck_X", theX);
      PlayerPrefs.SetFloat("deck_Y", theY);

      EndMultiIconStatus(); //End without the need to click on icon
    }
   }
 }

I have tested with a local LIST as well, gives the same result.

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).

Uhhhh i must have got brain damage and spent hours to get it :frowning: …unbelivable

Thanks :slight_smile: