Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 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"?

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

i want to shuffle randomly 5 blocks as shown in image @Harinezumi

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.

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

Instantiate a button at mouse click position, 0 Answers

Instantiated Object Wont save a reference to the prefab, instead sets to itself 1 Answer

How to Spawn after checking if the clones are destroyed. 1 Answer

[Solved] How to instantiate GameObject with different rigidbody parameters 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