Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Trying to get the positions of an array of gameobjects.

I have an array of 34 unique gameObject "Tiles" and an array of 34 gameObject "placeholders". I am trying to place my shuffled tiles into the placeholder positions without repeats. I am getting lost with finding my "pos" vector3 for my Instantiate. I am basically a complete noob! (Sorry) Any help would be greatly appreciated!

Here is what i have so far.

using UnityEngine;

public class TileScript : MonoBehaviour {

  public GameObject[] movableTiles;

  public GameObject Tile;

  public GameObject[] placeholders;

  public Vector3 pos;


  void Start()




  // Update is called once per frame

  void Update()



  public void ShuffleTiles()


    int mtl = movableTiles.Length;

         for (int i = 0; i < mtl - 1; i++)


      int rnd = Random.Range(i, mtl);

      Tile = movableTiles[rnd];

      movableTiles[rnd] = movableTiles[i];

      movableTiles[i] = Tile;

       placeholders = GameObject.FindGameObjectsWithTag("Placeholder");

      Vector3 pos = new Vector3 ();


      //placeholders is an array of gameobjects to put my now shuffled tiles in

      //i have 34 tiles and 34 gameobjects, each with its own unique vector3

      //i need to place the tiles in each position of the array of gameobjects named placeholders

      Instantiate(Tile, pos, Quaternion.Euler(0, Random.Range(0, 4) * 90, 0));

Best Answer

    edited July 2020 Accepted Answer

    Use vector3[] for placeholder

    In the start method where everything is aligned try looping through all objects and store there vector3 location in the vector3 placeholder array

    Then in the shuffle function make an array name it available place and assign the placeholder array to it then start looping through all object you have to shuffle.

    In the loop get a random index of the newly made array the available places and assign that vector 3 position to the object then remove that option from the array(if you are familiar with the slice function from javascript) you can make your own slice function and so everytime you select a random index for the array the used position will never gets repeated

    Tell me if you dont know how to make a slice function


Sign In or Register to comment.