- Home /
Moving a group of objects at once in runtime.
I have a series of scripts that allow a part to be dragged around my scene. If two parts collide, they are both childed in a gameobject called Group1. But when I then go to drag them, they are dragged separately, but they are still in the same 'group'. Here is the code I use for attaching the objects:
using UnityEngine;
using System.Collections;
public class ObjectAttach : MonoBehaviour {
public Transform Group1;
void Start()
{
Group1 = GameObject.FindWithTag("Group").transform;
}
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag=="Part")
{
collision.transform.parent = Group1;
}
}
}
Answer by Spinnernicholas · Dec 03, 2013 at 09:21 PM
I think you are selecting the individual objects. You need to have a logical way to select the group object for dragging as opposed to the child objects. It's all about what Transform object you are modifying. If your dragging script is not on the group object, make sure it is changing the transform of the group object.
public Transform groupTransform;
void moveGroup(Vector3 moveVector)
{
groupTransform.position += moveVector;
}
If you are using physics, you have to apply the physics to the group.
Your answer
![](https://koobas.hobune.stream/wayback/20220613123329im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Objects with colliders going through walls and each other. 3 Answers
Changing Rigidbody Drag on Collision 1 Answer