- Home /
how can i shuffle cubes
i have 15 cubes and 1 empty gameobject,i want to shuffle them so they change their position on start of the game. i tried this snippet but nothing is happening.
var cube1 : GameObject;
var cube2 : GameObject;
var cube3 : GameObject;
var cube4 : GameObject;
var cube5 : GameObject;
var cube6 : GameObject;
var cube7 : GameObject;
var cube8 : GameObject;
var cube9 : GameObject;
var cube10 : GameObject;
var cube11 : GameObject;
var cube12 : GameObject;
var cube13 : GameObject;
var cube14 : GameObject;
var cube15 : GameObject;
var slot : Transform;
//var cubes: Vector3[] = new Vector3[16];
//var index : int = 0;
var cubes: Transform[];
function Start ()
{
ChangePosition();
// OnMouseUp();
}
function ChangePosition()
{
// for (var i = 0; i < cubes.Length; i++) {
// var j = Random.Range(0, cubes.Length);
// var tmp = cubes[i].position;
// cubes[i].position = cubes[j].position;
// cubes[j]. position = tmp;
cube1.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube2.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube3.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube4.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube5.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube6.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube7.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube8.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube9.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube10.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube11.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube12.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube13.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube14.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
cube15.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
slot.transform.position = new Vector3(Random.Range (1,4),Random.Range (1,4),10);
// }
}
function OnMouseUp() {
//if(Vector3.Distance(transform.position,slot.position)==1
if(Vector3.Distance(transform.position,slot.position)==1)
{
xtemp = transform.position.x;
ytemp = transform.position.y;
transform.position.x=slot.position.x;
transform.position.y=slot.position.y;
slot.position.x = xtemp;
slot.position.y = ytemp;
}
}
This code appears to just swap around entries in the cubes array. There's no code here that uses those entries to set any game object positions. I'm just guessing, but you may want your 'cubes' array to a Transform[]. Then you can swap around the positions of all the transforms.
Answer by robertbu · May 16, 2014 at 07:14 AM
I'm going to assume you are going to initialize the 'cubes' array through drag and drop in the inspector. And that array is initialized with game object from the hierarchy.
#pragma strict
var cubes: Transform[]; // Initialized in the Inspector
function Start () {
ChangePosition();
}
function ChangePosition() {
for (var i = 0; i < cubes.Length; i++) {
var j = Random.Range(0, cubes.Length);
var tmp = cubes[i].position;
cubes[i].position = cubes[j].position;
cubes[j]. position = tmp;
}
}
@ robertu sry,i tried your code but cubes not changing their positions
I think above code snippet work definitely. Although you don't set directly position of gameobject. You have to copy it into Vector3 and then apply it to next gameobject.
@ siddharth3322 if 1 will not set the position of the gameobjects,then how will they take their position..,m in learning phase thats why i m asking..please guide me
Basically you know about position setting of gameobject or not?
Answer by siddharth3322 · May 16, 2014 at 09:23 AM
Lets give clear idea to you.
Vector3 shuffleArray = new Vector3[cubes.length];
Here, shuffleArray holds position of each cube means initial position of each cube. Then we interchange position of this shuffleArray means using shuffle algorithm we perform this task.
Now we have new position array ready. We apply it to cubes.
for(int i=0;i<shuffleArray.Length;i++)
cubes[i].transform.position = shuffleArray[i];
Now each cube has new position because of shuffling.
Assets/$$anonymous$$ovement.js(103,8): UCE0001: ';' expected. Insert a semicolon at the end.
this error occurs when i declare.
Vector3 shuffleArray = new Vector3[cubes.length];
You have to do some task your way. At present I written code in c# and you try to run this code in java script.
i have converet that..,but 1 last ques how to remove this error Cannot convert 'UnityEngine.Vector3[]' to 'UnityEngine.Vector3'
Thank you, at least now you understand and free me.
Vector3 []shuffleArray = new Vector3[cubes.length];
I have created array in roughly.
Your answer
![](https://koobas.hobune.stream/wayback/20220613145144im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Curving an Horizontal Slider 2 Answers
Create Animation in unity3d only on one axis 0 Answers
Float variable not even updating? 1 Answer
Weird Animation clip curves tangents 1 Answer
How can I make a Lerp move in an arc instead of a straight line? 2 Answers