Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 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 /
This question was closed Feb 03, 2021 at 08:38 AM by DiasPTW for the following reason:

Other

avatar image
0
Question by DiasPTW · Feb 01, 2021 at 12:18 PM · listsgrid based gamecardsgridmoveswapping

How do I swap two gameObjects order in a list?

So I have this list filled with "instructions" for tile-based movement, but currently it is generated randomly and you cannot swap the positions of said instructions so the gameObjects just go around and move randomly. I'd like to be able to move those instructions around in order for the player to be able to move those gameObjects in their desired order.

This is how the movement "cards" are given and shuffled:

 void DarPeçasNecessarias() //Da exatamente as peças necessárias para o jogador terminar o nivel
     {
         if (direçao.x > 0)
         {
             numRight = (int) direçao.x;
             for (int i = 0; i < numRight; i++)
             {
                 Peçass.Add("Right");
             }
             if (direçao.y > 0)
             {
                 numUp = (int) direçao.y;
                 for (int i = numRight; i < numRight + numUp; i++)
                 {
                     Peçass.Add("Up");
                 }
             }
             else if (direçao.y < 0)
             {
                 numDwn = (int) - direçao.y;
                 for (int i = numRight; i < numRight + numDwn; i++)
                 {
                     Peçass.Add("Down");
                 }
             }
             else { numDwn = 0; numUp = 0; }
 
         }
         else if (direçao.x < 0)
         {
             numLft = (int) - direçao.x;
             for (int i = 0; i < numLft; i++)
             {
                 Peçass.Add("Left");
             }
             if (direçao.y > 0)
             {
                 numUp = (int) direçao.y;
                 for (int i = numLft; i < numLft + numUp; i++)
                 {
                     Peçass.Add("Up");
                 }
             }
             else if (direçao.y < 0)
             {
                 numDwn = (int) - direçao.y;
                 for (int i = numLft; i < numLft + numDwn; i++)
                 {
                     Peçass.Add("Down");
                 }
             }
             else { numDwn = 0; numUp = 0; }
         }
         else { numLft = 0; numRight = 0; }
         //-----------------
         numPeçasNecessarias = numRight + numLft + numUp + numDwn;
         DarPeçasExtra();
     }
 
     void DarPeçasExtra() //Da o resto das peças
     {
         for (int i = numPeçasNecessarias; i < NumPeças; i++)
         {
             int nrg = Random.Range(1,5);
             //Debug.Log(nrg);
             if (nrg == 1)
             {
                 Peçass.Add("Up");
                 numUp++;
             }
             else if (nrg == 2)
             {
                 Peçass.Add("Down");
                 numDwn++;
             }
             else if (nrg == 3)
             {
                 Peçass.Add("Left");
                 numLft++;
             }
             else
             {
                 Peçass.Add("Right");
                 numRight++;
             }
         }
 
         ShuffleArray();
     }
 
     void ShuffleArray() //Baralha o array
     {
         string Temp = "Temp";
         for (int i = 0; i < Peçass.Count; i++)
         {
             int rnd = Random.Range(0, Peçass.Count);
             Temp = Peçass[rnd];
             Peçass[rnd] = Peçass[i];
             Peçass[i] = Temp;
         }
         //FillImages();
     }

I'd like to know where do I go from here, currently there are temporary visual indicators that show the order of the cards and movement, but I'd like to move them around so that it can move freely, eg: alt text

Any help would be greatly appreciated! Thank you!

example.png (72.4 kB)
Comment
Add comment · Show 1
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 DiasPTW · Jan 30, 2021 at 12:36 PM 0
Share

These visual indicators consist of buttons. The FillImages() method just changes their sprite according to the list instruction

The method DarPeçasNecessarias() basically gives the player the exact amount of cards and instructions to finish the level.

The method DarPeçasExtra() just gives random other "cards" to fill the list and the Hotbar.

Sorry for not specifying it in the post itself!

2 Replies

  • Sort: 
avatar image
0

Answer by Llama_w_2Ls · Feb 01, 2021 at 01:54 PM

To swap items in an array or list, create a temporary value that stores variable a, set a to b, and set b to that temporary value. For example:

     void Swap(GameObject a, GameObject b)
     {
         GameObject temp = a;
         a = b;
         b = temp;
     }

@DiasPTW Is that what you needed? Your question sounds a little complicated.

Comment
Add comment · Show 1 · 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 DiasPTW · Feb 01, 2021 at 07:28 PM 0
Share

That is more or less what I want, my real question is basically how do I detect which 2 buttons/gameObjects I clicked and then change their orders in the list, sorry for not explaining it well.

avatar image
0

Answer by Wolfzoon · Feb 01, 2021 at 02:27 PM

It's not so clear to me what your question is. You obviously know how to swap List<T> items, as you do the same in your ShuffleArray function. Nor do I really get what DarPeçasNecessarias does. Is it called on Clicking the gameObjects?

I see a bunch of "instructions" in your screenshot, these are each GameObjects with an InstructionScript attached I'd guess? But do they also know which position they have in the "List", I assume they are executed left-to-right, top-to-bottom?

In that case you would want these gameObjects to swap positions visually, and in the way you keep track of them, e.g.

 Vector3 posTemp = objectOne.transform.position;
 objectOne.transform.position = objectTwo.transform.position;
 objectTwo.transform.position = posTemp;
 
 var scriptOne = objectOne.GetComponent<InstructionScript>();
 var scriptTwo = objectTwo.GetComponent<InstructionScript>()
 Swap(scriptOne.Number, scriptTwo.Number);

@DiasPTW Do you need something like this?

Comment
Add comment · Show 1 · 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 DiasPTW · Feb 01, 2021 at 07:33 PM 0
Share

the DarPeçasNecessarias is called in the start function, I then added a comment to the Original post kinda explaining what those functions do, sorry for not including it in the post itself.

I don't really want to change the positions itself, I really only want to change the positions in the list because in the Update function ill update visually. I'm just a little confused because I can't really figure out how I can detect which 2 buttons/gameObjects i clicked and then see their place on the list and then change their positions on said. Sorry for the poor explanation originally

Follow this Question

Answers Answers and Comments

111 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

Related Questions

Asymmetricly placed, data containing, predefined movement locations for a 2d, top down turn based tactical game that takes place on a topographic map 1 Answer

How do I swap gameObjects on a list by clicking in the UI? 1 Answer

Grid-based movement with auto-move 1 Answer

Collision with grid based movement 0 Answers

How do I add new instances of a object into a list? 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