Problem in placing my array pieces

First my apologies for my noob qualities.

I am in the process of setting up the levels for the game I’m creating. All the GameObjects are set up in the script as a GameObject array called ‘piece’. It is necessary to have 5 random pieces in position at the start of the game.
I have been using Andeeeee’s very useful Random generator that makes sure that the numbers are not repeated. Each piece has destinationX, destinationY and destinationZ variables, filled in the Inspector and the destinationPoints variable:

var destinationPoints: Vector3;
destinationPoints = Vector3(destinationX,destinationY,destinationZ);

…is used for the drag and drop when dragged by the mouse. Positioning the pieces without selecting them I’m using the if(piece[m] != null). Here’s the function:

function RandomiseSome()
	var rangeTop = 16;
	var numSamples = 5;
	var l: int[] = RandFuncs.SampleRemove(rangeTop,numSamples);
	for(m = 0; m<15; m++)
		if(m == l[0] || m == l[1] || m == l[2] || m == l[3] || m == l[4])
			if(piece[m] != null)
				transform.position = (destinationPoints);
				transform.rotation = Quaternion.Euler(rotationDestination);
	MainMenuGUI.nivel = 0;

I’m having a lot of problems with this and have tried it many different ways. Each time I get only one piece moving (always the same piece).

I have searched the forum for a long time to no avail. Any help would be greatly appreciated.

Is the script attached to the only piece moving? If so, what about:

if(piece[m] != null) {

    piece[m].transform.position = ... 
    piece[m].transform.rotation = ...  


Otherwise the transform.position and transform.rotation have an implicit ‘this’ reference and will only affect the transform of the gameObject that the script is attached to.

Ok, looks like a couple of things to consider.

The first step isn’t a cure for the issue at hand (regarding the random pieces moving to the same place) but should help to eliminate redundancies that may make finding the problem more difficult.

The second step may or may not get the problem somewhere.

Make sure you back up your work first!

Step 1: If this script is on all of the pieces, and the pieces are being referenced by both tag and inspector, there are likely to be referential redundancies. A suggestion would be:

– Create an empty gameObject in the scene and attach the script discussed above to it.

– Keep the .FindGameObjectsWithTag() method in this script to reference the pieces, but don’t assign pieces to this script in the Inspector by dragging (they are already referenced).

– Remove the script above from each of the pieces.

Step 2: Put your Vector3 called ‘destinationPoints’ on each piece and then reference it, as follows:

– Create a blank script, lets call it ‘destination.js’, with just the variable ‘destinationPoints’ and attach it to each of the pieces:

public var destinationPoints: Vector3;

– Be sure to fill in the destination points XYZ in the inspector for each piece. Don’t assign these by script (they’re already assigned in the inspector).

– Now, go back to the script on the empty gameObject from Step 1 and substitute the following conditional (same one discussed previously):

if (piece[m] != null) {

	var dest : destination = piece[m].GetComponent(destination);
	var destPoints : Vector3 = dest.destinationPoints;

	piece[m].transform.position = destPoints;
	piece[m].transform.rotation = Quaternion.Euler(rotationDestination);

There are ways to potentially make this more efficient (e.g., declare vars outside of loops, etc.) but a good way to start is to make it work – optimize later!

Many thanks PracticePad Inc.! It works!!!

I spent some time going through the scripts to follow your advice, and cutting down on the amount of code relevant to what you were telling me. There is a script called DragDrop (for that very purpose) and I used that as the script with the destinationPoints. I got rid of the destinationX, destinationY, destinationZ floats as well as the destinationPoints = Vector3(destinationX,destinationY,destinationZ); as, as you stated, it is not necessary as the X, Y and Z of destinationPoints appears on the Inspector anyway (where I entered the destination X, Y and Z of each piece). I detached all except for the DragDrop scripts from the GameObjects, and attached the script to an empty GameObject.

In answer to your question, yes, the pieces that can be chosen know where they should go, so that’s fine. I applied it to the rotation in the same way:

var rotation : DragDrop = piece[m].GetComponent(DragDrop);
var rotationDest : Vector3 = rotation.rotationDestination; 
piece[m].transform.rotation = Quaternion.Euler(rotationDest);

and it worked too.

I do refer in the script in two different functions (one Update) to the destPosition and rotationDest so I’ve put the lines into each function and it works fine.

Many thanks!!!