How to make 5 GameObjects swap between 5 predetermined positions (1 gameobject occupy 1 position per time)?
First things first, I'm pretty much a newbie (I've started learning programming 2 weeks ago)... So, I have one simple code. It contains a variable, which I call "trocar", and two arrays: "posicoes", which contains 5 Vector2, and "tijolos", which are my 5 game objects. In another script, I'm incrementing my variable "trocar".
 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class TrocarBarra : MonoBehaviour
 {
     public static int trocar;
     public Vector2[] posicoes = new Vector2[5];
     public GameObject[] tijolos = new GameObject[5];
 
     void Update()
      {
         if (trocar == 10){
         // here goes the script I need
         trocar = 0;
          }
      }
 }
 
What I'd like to have in this script is: whenever "trocar" reaches the value 10, I swap my GameObjects "tijolos" so they occupy all of the 5 Vector2 "posicoes", in a way that only one of them can occupy a position per time.
I tried it with "if's" but it's not good, since there are 125 combinations. Also, I tried to create something like this:
         int pos1 = Random.Range(0, posicoes.Length);
         int pos2 = Random.Range(0, posicoes.Length);
         int pos3 = Random.Range(0, posicoes.Length);
         int pos4 = Random.Range(0, posicoes.Length);
         int pos5 = Random.Range(0, posicoes.Length);
         while (pos2 == pos1)
              {
                  int pos2 = Random.Range(0, posicoes.Length);
                 // etc etc
              }
until I had the following:
 tijolos[1].transform.position = posicoes[pos1];
 tijolos[2].transform.position = posicoes[pos2];
 tijolos[3].transform.position = posicoes[pos3];
 tijolos[4].transform.position = posicoes[pos4];
 tijolos[5].transform.position = posicoes[pos5];
but it won't work, since pos2, pos3, pos4 and pos5 can't be used inside the while loop within what I wrote above. With all said, I'd appreciate if anyone can help me!
Answer by Sawka · Aug 05, 2020 at 12:49 AM
Here's an update:
I managed to get this code for the last part:
 for (int i = tijolos.Length-1; i >= 0; i--)
         {
             int random = Random.Range(0, pos.Length);
 
             tijolos[i].transform.position = pos[random];
Now all of my objects are changing randomly, but they still overlay (I knew this would happen because the code allows overlay right now, I just wanted to share the new code lines).
Your answer
 
 
             Follow this Question
Related Questions
Position Array Returning null reference 1 Answer
Array transform.position from another script 1 Answer
Spawning limited GameObjects at a specific position not working 1 Answer
Is there a way to allow the user to change the position of the game object in game? 0 Answers
How do I access an object script variable that is in a 2D array? 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                