Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 NickMx · Feb 20, 2014 at 02:03 AM · arraysrandomizepositionsshuffle

Connect object's position to shuffled array

I would like to randomly assign a set of positions to some GameObjects. Example: I have 4 different kinds of Cubes, when the game starts,each one should go to one of 4 positions on a table (like a grid), and next time the game starts the cubes changes positions. (The Positions are the same, the objects just change places with eachother.) I have managed to create an Array of int numbers and shuffle that, I can see the orders of the numbers change in the inspector. (It was from the Unity manual example and a tutorial on Fisher Yates shuffle. What I would like to know, is it possible to connect the numbers from that Array to an Array of GameObjects transform postions? Like Array element [0] = GO [0] transform.position x,y,z... Or is it possible to make the Fisher Yates shuffle with an Array of Gameobjects or Transform or Vector3 Array? I Think I've searched around for Three Days on this so I would be deeply grateful for just some help... Here's what I have so far;

public int[] Numbers = new int [4];

 void Start ()
 {
     Shuffle (Numbers);
     
 }
 
 void Shuffle(int[] a)
 {
     
     for (int i = a.Length-1; i > 0; i--)
     {
         int rnd = Random.Range (0,i);
         
         int temp = a[i];
         
         a[i] =a[rnd];
         a[rnd] = temp;
     }
     
     for (int i = 0; i < a.Length; i++)
     {
         Debug.Log (a[i]);
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by robertbu · Feb 20, 2014 at 02:11 AM

Any of what you suggest is possible. I'd shuffle the array of game objects, or the array of positions. The only change you would have to make is the type:

 void Shuffle(GameObject[] a)
 {
  
     for (int i = a.Length-1; i > 0; i--)
     {
        int rnd = Random.Range (0,i);
        GameObject temp = a[i];
        a[i] =a[rnd];
        a[rnd] = temp;
     }
 }

Then you would just assign the positions in order:

    for (i = 0; i < goArray.Length; i++) {
        goArray[i].transform.position = positions[i];
    }

But you could do it with shuffling an array of numbers. Assume you had shuffled the array called num. You could do:

    for (i = 0; i < goArray.Length; i++) {
        goArray[num[i]].transform.position = positions[i];
    }
Comment
Add comment · Show 2 · 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 NickMx · Feb 21, 2014 at 09:06 AM 0
Share

Great! Thank you so much! I would have Upvoted the answer if I could. The first part I got all right at once, after some sleep I also managed to understand how to get the transform positions right too. I made a public Vector 3 Array variable named positions:
public Vector3 [] positions = new Vector3 [4]; and then it was easy to assign the cubes to those positions elements, and I adjusted the last bit of code (with "int" before i) and it worked perfect! (I added a debug where you can follow how they change, it is also visible in the inspector.)

for (int i = 0; i < Cubes.Length; i++) { Debug.Log("Cube number "+i+" is named "+Cubes[i].name); Cubes[i].transform.position = positions[i]; }

avatar image tejmayankpatel · May 30, 2021 at 10:45 PM 0
Share

This is the most underrated answer I've ever seen! This helped me perfectly! Thank you so much!

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

19 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

Related Questions

How do I randomize the order of spawn points without repeats? 1 Answer

Setting GUI textures 1 Answer

Find Active gameobject with tag and store in a List/Array as a Transform? 3 Answers

Iterating through shuffled/randomized array issue 0 Answers

Spawning - How to only have one object at a spawn at a time? 0 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