- Home /
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.
Answer by PracticePad Inc. · Dec 07, 2012 at 12:48 AM
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!
Thank you for your help PracticPad Inc.
In response to your question, the script is attached to all the pieces. I made the GameObject pieces as variables on the script and using: piece = GameObject.FindGameObjectsWithTag("Piece"); imported them. Then I dragged each of the GameObjects across to the appropriate place on the script's Inspector. I attached the script to all of the pieces. Then, selecting each of the GameObjects in turn, I went to the list of the GameObjects within the script and only dragged across that GameObject, leaving the rest blank - (None(Game Object)).
I tried your advice of having piece[m] at the beginning of:
piece[m].transform.position = (destinationPoints);
piece[m].transform.rotation = Quaternion.Euler(rotationDestination);
It was interesting what happened. All 5 of the random pieces moved which was great. But they all moved to the same place. I mentioned in my first post that only one piece had moved to it's correct place. Now all 5 pieces moved to that place (whether that part was one of the parts being moved or not). So the parts being moved is correct but the place it's being moved to seems to be locked on one position. Any ideas on getting them to go to the correct position?
I've discovered as well that the position it seems to be locked onto is that of piece[0].
btw - I haven't addressed the rotation in these snippets but the general idea is the same.
Also, I'm assu$$anonymous$$g that each of the pieces that can be chosen from 'knows' where it should go, as opposed to having 5 fixed places for the randomly selected pieces - this of course depends on how your game is designed to be played and affects what your solution ultimately will be.
Answer by PracticePad Inc. · Dec 06, 2012 at 07:19 AM
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.
Answer by BenedictH · Dec 10, 2012 at 12:47 PM
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!!!
Your answer
![](https://koobas.hobune.stream/wayback/20220613085025im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to display gameobjects when i pause the game? 1 Answer
objects in the same array wont move 0 Answers
Get a GameObject from a component 1 Answer
Find one inactive player (gameobject) 2 Answers
Multiple boards 0 Answers