Transform Position of all Objects from a List

Hi,

can someone please enlighten me what i do wrong? Im looking at this for quiet some time now and i dont get it.

I have a GameObject that can be dragged around. When i now press the F key i instantiate a second game object and add the new instantiatet and the already existing one into a list.

I now want both objects in that list to move while either one is dragged relative to their position. If object one is at 0,0,0 and object two is at 2,0,0 and i drag object one for example one the y axis, object two should move on the y axis aswell.

My code just adds the drag position to the object and then resets it.

My Object Code:

using UnityEngine;
using System.Collections;


public class dumdum : MonoBehaviour{

    GameController controller;

    void Start()
    {
        controller = GameObject.Find("GameController").GetComponent<GameController>();
    }

    void OnMouseDown()
    {
        controller.OnMouseDown(this.gameObject);       
    }
   
    void OnMouseDrag()
    {
        controller.OnMouseDrag(this.gameObject);
    }
   
    void OnMouseUp()
    {
        controller.OnMouseUp(this.gameObject);       
    }
}

And the controller code:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class GameController : MonoBehaviour {
   
    Vector3 distance;              
    float posX;                    
    float posY;              

    List<GameObject> moveUs;

    public GameObject lulu;


    void Start()
    {
        moveUs = new List<GameObject>();      
    }

    public void OnMouseDown(GameObject dumdum)
    {       
        distance = Camera.main.WorldToScreenPoint(dumdum.transform.position);
        posX = Input.mousePosition.x - distance.x;
        posY = Input.mousePosition.y - distance.y;       
    }

    public void OnMouseDrag(GameObject dumdum)
    {      
        Vector3 currentPosition = new Vector3(Input.mousePosition.x - posX, Input.mousePosition.y - posY, distance.z);
        Vector3 worldPosition = Camera.main.ScreenToWorldPoint(currentPosition);
        dumdum.transform.position = worldPosition;
       
        if (moveUs.Count > 1)
        {
            for (int i = 0; i < moveUs.Count; i++)
            {               
                if (moveUs[i] != null)
                {
                    moveUs[i].transform.position = worldPosition;
                }              
            }
        }       

        if (Input.GetKeyUp(KeyCode.F))
        {           
            moveUs.Add(dumdum.GetComponent<GameObject>());
            moveUs.Add(Instantiate(lulu, new Vector3(0, 0, 0), Quaternion.identity) as GameObject);
        }
    }
   
    public void OnMouseUp(GameObject dumdum)
    {
        Debug.Log(" " + moveUs.Count);
    }
}

Thank you in advance!

parent all objects dragged , which are in the list you mention , all to one gameobject , then unparent them again after dragging , i guess this way you wont need to iterate constantly over each element , given the oppisit solution that comes to mind.

nah, thank you :slight_smile: But fumbling around with parents just brings more problems then it solves. I used a parent approach before.