Simple Looping drag and drop game

hi, I’m trying to do a simple drag and drop game for my nephew (maybe can publish it in playstore). Because of that I will work with so many items I don’t want to write code for each of them. I want to make a loop to find them by their index number and move to it’s place with the same index number. this is the code I came up with. but it’s not working and giving me theese errors;
IndexOutOfRangeException: Index was outside the bounds of the array.
MouseDrag.Start () (at Assets/MouseDrag.cs:22)
Assets\DragAndDrop.cs(10,25): warning CS0649: Field ‘DragAndDrop.initialPosition’ is never assigned to, and will always have its default value null

I’m sure there are many other errors that I don’t know. is there any body here have an idea or could show me how to do it. I’m desperately need any kind of help now.

Here is the code I tried to write:

`public class MouseDrag : MonoBehaviour
{
public GameObject objects;

public Transform[] objectPlace;
public List <Vector2> initialPosition = new List<Vector2>();
private Vector2 mousePosition;
private float deltaX, deltaY;
int i = 0;
public static bool locked;

private void Start()
{ 	
	for(int i=0; i< objects.Length; i++) 
	
          initialPosition.Add(new Vector2());
		  initialPosition _= objects*.transform.position;*_

* }*

* void OnMouseDown()*
* { *

* metod(i);*

* deltaX = Camera.main.ScreenToWorldPoint(Input.mousePosition).x - transform.position.x;*
* deltaY = Camera.main.ScreenToWorldPoint(Input.mousePosition).y - transform.position.y;*

* }*
* void OnMouseDrag() {*
* if(!locked){*
* mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);*
* transform.position = new Vector2(mousePosition.x - deltaX, mousePosition.y - deltaY);*
* }*
* }*
* void OnMouseUp()*
{
_ if(Mathf.Abs(transform.position.x - objectPlace*.position.x) <= 0.5f &&
Mathf.Abs(transform.position.y - objectPlace.position.y)<= 0.5f)
{
transform.position = new Vector2(objectPlace.position.x, objectPlace.position.y);
locked = true;*_

* }*
* }*
* public int metod(int i)*
* {*
* for(int p = 0; p < objects.Length; p++)*
* if ( !locked)*
* { *
* RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);*

* if (hit.collider.gameObject == objects[p].gameObject){Debug.Log("index number is: " + objects[p].transform.position.ToString());}*
* p = i;}*
* return i;*
* }*
}

2 things wrong here on a wuick overview:

  • delete the line initialposition.add and ad directly the position = initialPosition.Add(objects*.transform.position);*
    - metod i is wrong, you call it in the update section with i. is never asigned or a valid int. delet int i in metod because its unecessary, instead return p directly
    Edit, Final working Script:
    using UnityEngine;
    using System.Collections.Generic;
    public class MouseDrag : MonoBehaviour{

public float distanceToEndPos = 1;
public List objects = new List();
public List initialPosition = new List();
public List desiredPosition = new List();
Vector3 mousePos;
Transform curObj;
int curIndex;
bool isDraging = false;

void Start(){
objects.AddRange(GameObject.FindGameObjectsWithTag(“DragAndDropObjects”));
foreach(GameObject g in objects){
initialPosition.Add(g.transform.position);
}
GameObject[] desObj = GameObject.FindGameObjectsWithTag(“DragAndDropEndPos”);
foreach(GameObject g in desObj){
desiredPosition.Add(g.transform.position);
}
}
void Update(){
mousePos = GetMousePos();
if(!isDraging){
GetRayCast();
}
if(isDraging){
DragAndDrop();
}
}
public void GetRayCast(){
RaycastHit2D hit = Physics2D.Raycast(new Vector2(mousePos.x,mousePos.y),Vector2.one);
if(hit.collider != null && Input.GetMouseButtonDown(0) && hit.transform.tag == “DragAndDropObjects”){
for(int i = 0; i<objects.Count;i++){
if(hit.collider != null && Input.GetMouseButtonDown(0) && hit.transform.gameObject == objects*){*
curIndex = i;
curObj = objects*.transform;*
isDraging = true;
}
}

}
}

public Vector3 GetMousePos(){
Vector3 mp = new Vector3();
mp = Camera.main.ScreenToWorldPoint(Input.mousePosition);
return new Vector3(mp.x,mp.y,0);
}
public void DragAndDrop(){
if(curObj != null){
if(Input.GetMouseButton(0)){
curObj.position = mousePos;

}
if(Input.GetMouseButtonUp(0)){
if(Vector2.Distance(mousePos,desiredPosition[curIndex]) <= distanceToEndPos){
curObj.position = desiredPosition[curIndex];
curObj = null;
isDraging = false;
}else{
curObj.position = initialPosition[curIndex];
curObj = null;
isDraging = false;
}
}
}
}
}

it still gives me this error;
Field ‘DragAndDrop.initialPosition’ is never assigned to, and will always have its default value null
I tried to call it on awake function but nothing changed
@dan_wipf