Search Unity

need help with transform position arrays

Discussion in 'Scripting' started by ThisIsSparta, Jan 30, 2015.

  1. ThisIsSparta

    ThisIsSparta

    Joined:
    May 4, 2014
    Posts:
    142
    Hi guys, I hope someone can help me out. I have 3 elements and I'm using a for cycle to go trought this 3 objects and move them to 3 different fixed positions...here is the script:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Spawn : MonoBehaviour
    5. {
    6.     public GameObject[] draggers;
    7.     public Vector3[] positions;
    8.  
    9.     void Start ()
    10.     {
    11.         draggers = GameObject.FindGameObjectsWithTag ("Draggers");
    12.  
    13.         for(int i = 0; i < draggers.Length; i++)
    14.         {
    15.  
    16.         }
    17.     }
    18.  
    19. }
    what I have to insert inside the brackets under the for cycle to let them alternate in this 3 positions?

    p.s.: notice that the vector3 is public since I can manage the 3 positions directly from the editor. thanks in advance
     
  2. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    Not sure what you mean by alternate. If you just want to assign them a position:
    Code (csharp):
    1.  
    2. draggers[i].position = positions[i];
    3.  
     
  3. ThisIsSparta

    ThisIsSparta

    Joined:
    May 4, 2014
    Posts:
    142
    actually using your script I have the error : UnityEngine.GameObject does not contain a definition for position"
     
  4. ThisIsSparta

    ThisIsSparta

    Joined:
    May 4, 2014
    Posts:
    142
    I fixed like this
    Code (CSharp):
    1. draggers[i].transform.position = positions [i];
    the problem is like this I always get the same positions... I want that this 3 objects in the first array switch between the 3 fixed positions in the other array every time the game start...

    if I'm not enough clear I'll try to explain better :) thx again
     
  5. ThisIsSparta

    ThisIsSparta

    Joined:
    May 4, 2014
    Posts:
    142
    I'm almost there! just need a little hint plz! I added this to the script

    Code (CSharp):
    1. int randomNumber = Random.Range(0,positions.Length);
    2.             draggers[i].transform.position = positions[randomNumber];
    and it actually randomize the positions. problem is sometimes the images overlap! how to fix this?

    I want just something like " take image one and place in 1 of these 3 spots - take image two and place in one of the two remaining spots - take image 3 and place in the remaining available spot."

    thx!