Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Himani_123 · May 16, 2014 at 06:38 AM · curveshuffle

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;
 
 }
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · May 16, 2014 at 06:46 AM 0
Share

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.

avatar image Himani_123 · May 16, 2014 at 06:49 AM 0
Share

@ robertbu can you tell me how to do that please???

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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;
     }
 }
Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Himani_123 · May 16, 2014 at 08:06 AM 0
Share

@ robertu sry,i tried your code but cubes not changing their positions

avatar image siddharth3322 · May 16, 2014 at 08:34 AM 0
Share

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.

avatar image Himani_123 · May 16, 2014 at 08:38 AM 0
Share

@ 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

avatar image siddharth3322 · May 16, 2014 at 08:42 AM 0
Share

Basically you know about position setting of gameobject or not?

avatar image Himani_123 · May 16, 2014 at 08:48 AM 0
Share

yes i set them as (0,0)(0,1)....(3,0)(3,1)(3,2)(3,3)

Show more comments
avatar image
1

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Himani_123 · May 16, 2014 at 09:38 AM 0
Share
 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];
avatar image siddharth3322 · May 16, 2014 at 09:41 AM 0
Share

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.

avatar image Himani_123 · May 16, 2014 at 09:44 AM 0
Share

i have converet that..,but 1 last ques how to remove this error Cannot convert 'UnityEngine.Vector3[]' to 'UnityEngine.Vector3'

avatar image siddharth3322 · May 16, 2014 at 09:48 AM 0
Share

Thank you, at least now you understand and free me.

Vector3 []shuffleArray = new Vector3[cubes.length];

I have created array in roughly.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges