How to move a group of objects to a certain position by onscreen button click

Hi everyone!

I’m trying to create a simple dressing up game for girls, and I need a button, which will undress all the clothes from a model and place to a certain position. For this purpose I create an array, which returns all clothes objects by tag, but I cannot change the position of this group. Here is my code:

using UnityEngine;
using System.Collections;

public class Button_undress_click : MonoBehaviour {
GameObject [] objs; // this is my array
public NewDrag drag; //this is a link to another script, which I'm using for dragging the clothes around the screen

void OnMouseUp()
{
objs = GameObject.FindGameObjectsWithTag("Cloth"); //getting the objects with "Cloth" tag into an array

foreach (GameObject obj in objs)
{
drag.current.x = 5; //the current point of a clothes object in a NewDrag script
drag.current.y = 0;
}
}

}

The game starts but pressing the button does nothing. The only one problem in a console is an attention about the “obj” variable is not using anywhere.

Help me please to correct my script.

You should listen to it. You’re not using “obj” anywhere. That really is your problem in its entirety. In that foreach loop, you should be doing something with the “obj” variable - whether that be moving obj itself or passing it along to a function that moves it, I don’t know, mostly because I don’t know what NewDrag is supposed to do.

What’s going on right now it that, if say you have 5 objects tagged “Cloth”, then it’s going to set drag.current to (5,0) five times, but this will have no relation to the tagged objects other than doing it that amount of times.

Well thanks, but how can I assign all my objects to one variable? Is there any global variables which can relate any of the objects in an array? I’ve search for such ones, but with no results. I’ve tried to use “element” instead of “obj”, but Unity has no such global variable…

I also tried to set a position of the objects directly (without additional scripts), but it doesn’t work too :frowning: Here is a code of my trying:

using UnityEngine;
using System.Collections;

public class Button_undress_click : MonoBehaviour {
	public GameObject [] objs;
	public Vector2 pos0;
	public Vector2 pos1;
	public Vector2 pos2;
	public Vector2 pos3;

	void Start(){
		objs = GameObject.FindGameObjectsWithTag("Cloth");
		}

	void Update() {
				pos0 = objs[0].transform.position;
				pos1 = objs[1].transform.position;
				pos2 = objs[2].transform.position;
				pos3 = objs[3].transform.position;
		}

	void OnMouseUp()
                {
		pos0.x = 5;
		pos0.y = 0;
		pos1.x = 5;
		pos1.y = 0;
		pos2.x = 5;
		pos2.y = 0;
		pos3.x = 5;
		pos3.y = 0;
		}
}

I have an array with 4 objects in it. I set a Vector2 variables for each of them. I can see coordinates changing in an Inspector, but MouseUp still doesn’t work even in this simple way :frowning: