Hi all,
I have the following code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using System.IO;
public class L1Controller : MonoBehaviour {
private GameObject FloorTile;
private GameObject WoodFloor;
private GameObject SteelFloor;
private GameObject MarbleFloor;
private float VerticalPos = 0.0f;
private int xRotHold;
private float xRot = 0.0f;
private float yRot = 0.0f;
private float zRot = 0.0f;
private GameObject NumberCube;
private GameObject HolePrefab;
private string[] SpriteList = new string[] { "zero", "one", "two", "three","four", "five", "six", "seven", "eight","nine"};
private string JSONString;
private static JsonData ItemData;
private string HoldTexture;
private int HoldFloor = 2;
// Use this for initialization
void Start () {
SteelFloor = (GameObject)Resources.Load("SteelFloor");
WoodFloor = (GameObject)Resources.Load("Floor");
MarbleFloor = (GameObject)Resources.Load("MarbleFloor");
NumberCube = (GameObject)Resources.Load("Number Cube");
HolePrefab = (GameObject)Resources.Load("Hole");
JSONString = File.ReadAllText(Application.dataPath + "/StreamingAssets/ZeroLevelData.json");
ItemData = JsonMapper.ToObject(JSONString);
FloorTile = WoodFloor;
for (float i = -7.0f; i <= 7.0f; i++)
{
for (float j = -9.0f; j <= 9.0f; j++)
{
int holdj = (int)j + 9;
int holdi = (int)i + 7;
HoldFloor = (int)ItemData["FloorArray"]["FloorSetup"][holdi][holdj];
if(ItemData["Level1"][HoldFloor]["Texture"].Equals("Wood"))
{
FloorTile = WoodFloor;
}
else if (ItemData["Level1"][HoldFloor]["Texture"].Equals("Marble"))
{
FloorTile = MarbleFloor;
}
if (ItemData["Level1"][HoldFloor]["Texture"].Equals("Steel"))
{
FloorTile = SteelFloor;
}
xRot = (float)(double)ItemData["Level1"][HoldFloor]["Rotation"]["x"];
yRot = (float)(double)ItemData["Level1"][HoldFloor]["Rotation"]["y"];
zRot = (float)(double)ItemData["Level1"][HoldFloor]["Rotation"]["z"];
Instantiate(FloorTile, new Vector3(i,VerticalPos,j), Quaternion.Euler(xRot,yRot,zRot));
}
}
for (int i = 0;i <=9; i++)
{
float xPole = (float)(double)ItemData["Cubes"]["CubePositions"][i]["x"];
float yPole = (float)(double)ItemData["Cubes"]["CubePositions"][i]["y"];
float zPole = (float)(double)ItemData["Cubes"]["CubePositions"][i]["z"];
Instantiate(NumberCube, new Vector3(xPole, yPole, zPole), NumberCube.transform.rotation);
NumberCube.transform.GetChild(0).GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>(SpriteList[i]);
NumberCube.transform.GetChild(1).GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>(SpriteList[i]);
NumberCube.transform.GetChild(2).GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>(SpriteList[i]);
NumberCube.name = "Cube " + i.ToString();
}
for (int i = 0;i<=9;i++)
{
float xPole = (float)(double)ItemData["Holes"]["HolePositions"][i]["x"];
float yPole = (float)(double)ItemData["Holes"]["HolePositions"][i]["y"];
float zPole = (float)(double)ItemData["Holes"]["HolePositions"][i]["z"];
Debug.Log("Pos " + xPole + " " + yPole + " " + zPole);
Debug.Log(i + " " + SpriteList[i]);
Instantiate(HolePrefab, new Vector3(xPole, yPole, zPole), HolePrefab.transform.rotation);
HolePrefab.transform.GetChild(3).GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>(SpriteList[i]);
HolePrefab.name = "Hole " + i.ToString();
Debug.Log(HolePrefab.name + " " + xPole + " " + yPole + " " + zPole);
}
}
}
If you look at the code instantiating the cubes and the holes, it is quite reasonable to expect that they would appear in the order 0,1,2,3,4,5,6,7,8,9. Except they don’t. They appear in the order 9,0,1,2,3,4,5,6,7,8. The debug statements show them in sequence from Hole 0 to Hole 9, but in the scene Hole 9 appears in the position designated for Hole 0, Hole 0 in the position designated to Hole 1 etc up to Hole 8 appearing where hole 9 should be.
Any thoughts always appreciated.