How can I make a drag-and-drop list of buttons fed using an array?

In my scene I have a number of items that are to be labeled by the user. I’ve created a script that makes a list of RepeatButtons based on the names of the items as I’ve assigned them in the inspector. I want the user to drag the button to the right item in my scene. So far, the buttons don’t drag.

Here is what I have:

var refObj : GameObject[];
private var i : int;
private var buttonWidth : int = 0;
var currentPositionX = new Array();
var currentPositionY = new Array();
var label;
var isDragging: boolean;
var subNum : int;


function Start()
{
	Shuffle(refObj);
	for (var i = 0; i < refObj.Length; ++i)
	{
		if (buttonWidth < refObj_.name.Length * 7)_
  •  {*
    

buttonWidth = refObj_.name.Length * 7;_
* }*
* }*

}

function OnGUI ()
{
* label = new Rect [refObj.Length];*
* for (var i = 0; i < refObj.Length; ++i)*
* {*
_ currentPositionX = 10;
currentPositionY = 70 + (i * 30);
label = Rect (currentPositionX, currentPositionY*, buttonWidth, 30);*_

if(GUI.RepeatButton(Rect(label_), refObj*.name))
{
if(Input.GetMouseButton(0))
{
isDragging = true;
subNum = i;*_

* }*
* }*

* }*
* if (isDragging)*
{
* label[subNum].x = Event.current.mousePosition.x;*
* label[subNum].y = Event.current.mousePosition.y;*
* }*

}

function Update()
{
* if (Input.GetMouseButtonUp(0))*
* isDragging = false;*
}

function Shuffle(refObj : GameObject[])
{
for (var i = 0; i < refObj.Length; ++i)
{
var r = Random.Range(0, refObj.Length);
var tmp = refObj*;*
refObj = refObj[r];
refObj[r] = tmp;
}
}

Below is the fixed code.

The problem in the original code was that in OnGUI, when isDragging is true, label[subNum] is assigned the current mousePosition values but the next time OnGUI is called, those values are overwritten in line 31.

So I moved lines 29,30,31 into the for-loop in Start, as those values only need to be initialized once. I also moved line 26, which initializes the label array, into Start.

#pragma strict

var refObj : GameObject[];
private var i : int;
private var buttonWidth : int = 0;
var currentPositionX = new Array();
var currentPositionY = new Array();
var label : Rect[];
var isDragging: boolean;
var subNum : int;
 
 
function Start()
{
    Shuffle(refObj);
    
    label = new Rect [refObj.Length];
    
    for (var i = 0; i < refObj.Length; ++i)
    {
       if (buttonWidth < refObj_.name.Length * 7)_

{
buttonWidth = refObj_.name.Length * 7;_
}

currentPositionX = 10;
currentPositionY = 70 + (i * 30);
label = Rect (currentPositionX_, currentPositionY*, buttonWidth, 30);
}*_

}

function OnGUI ()
{
for (var i = 0; i < refObj.Length; ++i)
{
if(GUI.RepeatButton(Rect(label_), refObj*.name))
{
if(Input.GetMouseButton(0))
{*

isDragging = true;
subNum = i;_

}
}

}
if (isDragging)
{
label[subNum].x = Event.current.mousePosition.x;
label[subNum].y = Event.current.mousePosition.y;
}

}

function Update()
{
if (Input.GetMouseButtonUp(0))
isDragging = false;
}

function Shuffle(refObj : GameObject[])
{
for (var i = 0; i < refObj.Length; ++i)
{
var r = Random.Range(0, refObj.Length);
var tmp = refObj*;*
refObj = refObj[r];
refObj[r] = tmp;
}
}