Loop being offset by 1

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.

How is your JSON defined? I’d assume it’s the order they are parsed out of the JSON and the order they end up being put into the List inside ItemData.

And your JsonMapper code - could be in there too.

You should be returning your Instantiate command into a variable (of gameobject, or suitable type) and then doing the remaining code on it… this would be how you manipulate the instance you just created.

Ex:

GameObject hole = Instantiate(HolePrefab, new Vector3(xPole, yPole, zPole), HolePrefab.transform.
hole.transform.GetChild(3).GetComponent<SpriteRenderer>().sprite = Resources.Load<Sprite>(SpriteList[i]);
hole.name = "Hole " + i.ToString();
Debug.Log(hole.name + " " + xPole + " " + yPole + " " + zPole);

Otherwise, I think you’re just changing your prefab, then the next instance will get the values you just set, and so on…

Return your Instantiated object into an instance, then working on the instance in following commands. I assume the rest of your code in that statement is fine, since you never mentioned issues there.

1 Like

The order they are being parsed in is the correct order. All the JSON contains is the co-ordinates. The first is Hole or Cube 0. Let me put it this way. The JSON results are shown in the debugs and based on that Hole 9 should be at 6.0, 0,0, 7.0, but it shows at -6.5, 0.0, 7.0 which are the co-ords for Hole 0, again correctly reflected in the debugs but positioned at .5.0,0.0,7.0 .

I

I’ll give that a try. Thanks.

@cstooch Worked perfectly. Thanks a lot.

You bet