Im trying to create a floor out of 4 rooms in one line. Basically I just have to instantiate them and set their position. Im doing this in a for-loop. When I only use one prefab (no random necessary) i can create the 4 rooms and everything works. But when i try to create 4 random rooms out of 3 prefabs the positioning is not right. I mean sometimes it is correct but sometimes one of the rooms has the same position as another room. I cant understand that because I multiply my x-coordinate with the variable from the loop (i), that would mean the variable would get used sometimes twice. I think there is something with my Random.Range command because its only not working when I try to make it random. Here is my code for this:
public int[] Room = new int[5];
public new Vector3 pos;
int xpos = 54;
int zpos = 0;
public GameObject[] R = new GameObject[3];
// Use this for initialization
void Start () {
for(int i = 1 ; i < 5 ; i++ ){
Room *= Random.Range (0, 3);*
_ xpos = 54*i;_
_ if (R [Room*].GetComponents ().Length < 1) { //Add MeshCollider if not already added*_
_ R [Room*].AddComponent ();
}
Instantiate (R [Room]); //create Room*
R [Room*].name = āā + xpos; //rename Room*_
* pos = new Vector3 (xpos, 0, zpos);*
_ R [Room*].transform.position = pos; //position Room*_
* }*
* }*
This is the whole script. I have added three prefabs to the GameObject array. I multiply āiā with 54 because 54 is the size of my room.