I am making a "build a tower" game. In my game I am texturing the cube sides randomly, so every new cube has random textures on every side. The new cubes are created by the script attached to "gamemanager" empty gameobject. This gamemanager attaches the "TextureController" script to every new cube. This script is responsible for creating texture atlas and picking UVs for every side of cube. Textures are loaded and stored in "TextureContainer" script (also attached to "gamemanager") that has a method that returns random array of textures. This works great, and as planned except one thing. The first (base) cube is not created by script but set by me. It has a different script (lets call it "baseCubeTextures"), that has to get array from "gamemanager", create own textureatlas and set UVs to the sides of the cube. But the thing is that the textures of the base cube are always same as of first created cube by script. To make it a bit clearer:
GameManager has a script GameManagerScript with method CreateNextCube()
with a line there:
playerCube.AddComponent<TextureController>().atlasTextures = GetComponent<TextureContainer>().getTextures(TextureSize.Ten);
it also has a TextureContainer
script atttached.
TextureContainer
has a method that returns random array of textures:
public Texture2D[] getTextures(TextureSize textureSize)
{
switch (textureSize)
{
case TextureSize.Ten:
textures[0] = textures10x6[Random.Range(0, textures10x6.Length)];
textures[2] = textures10x6[Random.Range(0, textures10x6.Length)];
textures[4] = textures10x6[Random.Range(0, textures10x6.Length)];
textures[5] = textures10x6[Random.Range(0, textures10x6.Length)];
break;
case TextureSize.Eight:
break;
case TextureSize.Six:
break;
case TextureSize.Four:
break;
case TextureSize.Two:
break;
case TextureSize.One:
break;
default:
break;
}
textures[1] = topBottomtextures[Random.Range(0, topBottomtextures.Length)];
textures[3] = topBottomtextures[Random.Range(0, topBottomtextures.Length)];
return textures;
}
THe base cube has a script that more or less looks like this:
public GameObject gameController;
private Texture2D[] atlasTextures = new Texture2D[7];
private Texture2D atlas;
Mesh mesh;
Vector2[] originalUVs;
private Rect[] atlasUVs;
// Use this for initialization
void Start()
{
atlasTextures = gameController.GetComponent<TextureContainer>().getTextures(TextureSize.Ten);
// code for setting UVs
}
So why do I have same textures from getTextures() method? Is it because I call this method almost at the same time (method is called in Start() of both scripts)? I read that it might happen with random number generators. Can it be somehow avoided here?
Aucun commentaire:
Enregistrer un commentaire