• 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 jayesh4520 · Jan 25, 2018 at 02:02 PM · unity 5instantiateswapshuffle

i want to swap my blocks

alt text

108337-block.png (3.6 kB)
Comment
Add comment · Show 3
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 jayesh4520 · Jan 25, 2018 at 02:04 PM 0
Share

assume that block has diffrent color

avatar image Harinezumi · Jan 25, 2018 at 02:15 PM 0
Share

A bit more information would be welcome. Which blocks do you want to "swap"? And what do you mean by "swap"?

Show more comments

1 Reply

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

Answer by Harinezumi · Jan 25, 2018 at 02:58 PM

Assuming that what you mean is to shuffle the game objects as with a deck of cards, you need a shuffle algorithm, and assigning the positions:

  public void ShuffleObjects(GameObjects[] toShuffle) {
       // store the original position of each element
       Vector3[] positions = new Vector3[toShuffle.Length];
      for (int i = 0; i < toShuffle.Length; ++i) { positions[i] = toShuffle[i].transform.position; }

      Shuffle(toShuffle);

      // assign the original positions to the shuffled array
      for (int i = 0; i < toShuffle.Length; ++i) { toShuffle[i].transform.position = positions[i]; }
  }

 public static void Shuffle(GameObject[] array) {
     int count = array.Length;
     int last = count - 1;
     for (int i = 0; i < last; ++i) {
         // select a random index to move to
         int randomIndex = UnityEngine.Random.Range(i, count);

         // swap the elements
         GameObject temp = array[i];
         array[i] = array[randomIndex];
         array[randomIndex] = temp;
     }
 }

See this post for a more generic shuffling solution.

Comment
Add comment · Show 3 · 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 jayesh4520 · Jan 25, 2018 at 05:04 PM 0
Share

I A$$anonymous$$ NEW I DONT $$anonymous$$NOW WHERE TO PUT THIS CODE to blocks?? @Harinezumi

avatar image Harinezumi jayesh4520 · Jan 25, 2018 at 06:02 PM 0
Share

Create a script and add the above code to it. Then when you want to shuffle your objects, call ShuffleObjects(yourArrayOfObjects); on this script. You need to create yourArrayOfObjects, the easiest way being declaring a public GameObject[] yourArrayOfObjects; and in the inspector assigning your objects to it.
This should get you going. I strongly recommend that you look up some basic tutorials for Unity to make it easier for you to understand the answers.
Good luck!

avatar image jayesh4520 Harinezumi · Jan 27, 2018 at 02:04 PM 0
Share

alt text

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class rop : $$anonymous$$onoBehaviour {

 public GameObject[] yourArrayOfObjects;
 public void ShuffleObjects(GameObjects[] toShuffle) {
        // store the original position of each element
        Vector3[] positions = new Vector3[toShuffle.Length];
       for (int i = 0; i < toShuffle.Length; ++i) { positions[i] = toShuffle[i].transform.position; }
       Shuffle(toShuffle);
       // assign the original positions to the shuffled array
       for (int i = 0; i < toShuffle.Length; ++i) { toShuffle[i].transform.position = positions[i]; }
   }
  public static void Shuffle(GameObject[] array) {
      int count = array.Length;
      int last = count - 1;
      for (int i = 0; i < last; ++i) {
          // select a random index to move to
          int randomIndex = UnityEngine.Random.Range(i, count);
          // swap the elements
          GameObject temp = array[i];
          array[i] = array[randomIndex];
          array[randomIndex] = temp;

      }
  }

} any problem in this code

capture.png (10.1 kB)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

163 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 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 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 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 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 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 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 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

I want to swap 5 blocks randomly on same position. 2 Answers

How can I Instantiate two new GameObjects at the same time? (Attempting Asteroids style game) 2 Answers

How do i Instantiate gameObjects in between multiple points? 2 Answers

Access a clone of an instantiated object - not all the clones? 2 Answers

Instantiate Problem ( RandomRange) 1 Answer


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