- Home /
Array of arrays of Textures
Hi, guys!!! I'm trying to make an Array of arrays of the textures and choose the Array randomly on changing the camera position. I've made the textures to change randomly from the list, and apply them to the walls, but actually I need arrays.
Example: There are 3 Arrays (of arrays): Red, Blue and Green - textures with the same wallpapers but containing different pictures and objects on the background. Each Array has 4 arrays (cause there are 4 walls). This way each wall receives the same wallpaper but with different pictures and objects. Every time the scene refreshes Unity chooses randomly the Array (Red, Blue or Green).
If anyone could help that would be fantastic!! Thank you!
Now the code looks like this:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
public class GenerateRoom : MonoBehaviour {
public Transform []slots ;
public int sizeOfList;
public List<GameObject> allObjects;
public List<GameObject> currentObjects;
public GameObject[] roomWalls;
public List<Texture2D> wallTextures;
void Start () {
CreateObjects ();
ChangeTexture ();
}
void Update () {
}
public void CreateObjects(){
System.Random rnd = new System.Random();
Transform[] MyRandomArray = slots.OrderBy(x => rnd.Next()).ToArray();
int i = 0;
foreach (Transform place in MyRandomArray) {
int idx = UnityEngine.Random.Range (0, allObjects.Count - 1);
GameObject obj = Instantiate (allObjects[idx], place.position, place.rotation) as GameObject;
obj.transform.position = place.position;
currentObjects.Add(obj);
allObjects.RemoveAt(idx);
i++;
if (i >= 8) break;
}
}
public void ChangeTexture(){
foreach (GameObject wall in roomWalls ) {
int index = UnityEngine.Random.Range (0, wallTextures.Count-1);
Texture2D newTexture = wallTextures[index];
wall.renderer.material.mainTexture = newTexture;
wallTextures.RemoveAt(index);
}
}
}
What is the question? what are you having issues with specifically? Are there exceptions being thrown?
Answer by Jeff-Kesselman · Jun 09, 2014 at 02:13 PM
Texture2D[][] textures = new Texture2D[3][4];
or
Texture2D[,] textures = new Texture2D[3,4];
Take your pick.
see http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx for more details
Answer by molokolom · Jun 18, 2014 at 05:41 PM
In the end I just did like this
public void ChangeTexture(){
int index = (((int)(UnityEngine.Random.value * 3)) % 3) * 4;
for (int i = 0; i < 4; i++) {
roomWalls[i].renderer.material.mainTexture = wallTextures[index+i];
}
}