GameObject not following its Transform's Vector

Hi,

I know that my question is very weird, but that’s mainly because I do not really know what is going wrong.
The point of my script is to do a crudely simple drag & drop system:


  1. When you click a selectable GameObject, well… it is selected. These GameObjects will be referenced as datasets, for my comfort ;
  2. When the Fire1 button is lifted, the position of the dataset is checked:
  3. If it is on near a “slot”, a dropable position, the dataset moves to the center of the Slot’s GameObject. If not, the dataset’s position simply becomes Vector3.zero.

This works by having a dataset class which takes in its associated GameObject and its position. That’s redundant, but just for safety. Also have a slot class, with a position and and boolean that is true if the slot is occupied, false if its free.
Roughly, its just a classic constructor:

public class Slot
{
   public Vector3 position ;
   public bool isOccupied ;

   public Slot(Vector3 _position, bool _isOccupied)
   {
        position = _position ;
        isOccupied = _isOccupied ;
   }
}

And same for the datasets.

Then the Drag & Drop scripts starts by creating a list of datasets, and of slots which are taken from the scene.
And then interesting part (sorry its a wee long…):

//A very rough drag and drop system:
        if (Input.GetButtonDown("Fire1"))
        {
//When the fire1 button is pressed, we loop through our list of datasets to assess which one has been selected.
            for (int i = 0 ; i < nb_datasets ; ++i)
            {
//isClicked is just the function I use to check if the object is clicked. Didn't use unity's raycast for performance reasons. Or just for fun.
                if (isClicked(datasetList*.pos,cam))*

{
//The selection boolean keeps track of whether a dataset is currently being selected.
selection = true ;
//selectedDS allows to keep track of which dataSet has been selected.
selectedDS = datasetList ;
dropPosition = Vector3.zero ;
}
}
}

if (Input.GetButtonUp(“Fire1”) && selection)
{
selection = false ;

//Get the screen position at which the object is dropped, to compare it with the slot’s screen position.
dropPosition.x = Input.mousePosition.x/Screen.width ;
dropPosition.y = Input.mousePosition.y/Screen.height ;
for (int i = 0 ; i < nb_datasets ; ++i)
{
//Compute the distance between the drop point and a slot:
setSlotDist = distance(worldToCamPos(slotList*.position,cam),dropPosition) ;*

//If the dataset is dropped sufficiently close to the slot’s transform, and that the slot is not occupied, then the dataset is placed in the slot.
if (setSlotDist < slctArea && !slotList*.isOccupied)*
{
datasetList[selectedDS.index].pos = slotList*.position ;*
datasetList[selectedDS.index].dataset.transform.position = datasetList[selectedDS.index].pos ;
slotList*.isOccupied = true ;*
}
else
{
datasetList[selectedDS.index].pos = Vector3.zero ;
datasetList[selectedDS.index].dataset.transform.position = datasetList[selectedDS.index].pos ;
slotList*.isOccupied = false ;*
}
}
}
Similarly to the above, if the selection button is lifted, then the code checks if it’s sufficiently close to any of the slots for it to placed in a slot. If not, the dataset goes to (0,0,0) world coordinates.
----------
And here comes my problem!!
For some reason, the code works with only one of the slots. When I try to drop the datasets in the other slots, they are sent to (0,0,0).
Now here is the magic:
After some quick debugging, I found out that the code still “thinks” the game object to be into the slot of interest. The position that is registered by the dataset class is indeed that of the slot, the class’s game object position too, and the slot is set to occupied state.
But… the dataset goes to (0,0,0)… Except for only one of the slots.
----------
Well that is it for me! Thank you very much for your attention, and hope to hear from you!
Thanks!

Without checking exactly everything, my suggestion is to just save the index like this

selectedDS = i; //selectedDS should be an *int*

Keeping the index in two places like that seems dangerous and unnecessary. Try to eliminate DataSet.index so

datasetList[selectedDS.index] => datasetList[selectedDS]

That’s my guess, but you are not showing the code for where you create the DataSet array.
Also it seems odd that you loop i=0…nb_datasets but use datasetList[selectedDS.index] and slotList*. Is dataset list and slot list mapped 1 to 1? If the problem persist you should show the code where you create the two lists.*