1
\$\begingroup\$

Using Unity 5.1 and was following a tutorial for generating a grid for a Minesweeper-style game. Link to the tutorial for reference: http://gamedevelopment.tutsplus.com/tutorials/build-a-grid-based-puzzle-game-like-minesweeper-in-unity-setup--cms-21361

Tutorial is Unity 4, but I don't think it should matter.

Here's my code:

public GameObject tilePrefab;
public int numOfTiles = 10;
public float distanceBetweenTiles = 1f;

void Start () {
    CreateTiles();
}

void CreateTiles () {
    float xOffset = 0f;

    for (int tilesCreated = 0; tilesCreated < numOfTiles; tilesCreated++) {
        Vector3 positionVar = new Vector3(transform.position.x + xOffset, transform.position.y, transform.position.z);
        xOffset += distanceBetweenTiles;
        Instantiate(tilePrefab, positionVar, transform.rotation);
    }
}

Essentially, the problem is that when I coded the spacing algorithm, Unity decided to ignore the spacing of 1f, but instead do 10,000. No matter the number I substitute for "distanceBetweenTiles", Unity spaces the cubes by 10,000 points. I'm assuming it has to do with how Unity is handling the float numbers, but I'm not quite sure, considering the number I substitute doesn't seem to matter. I've included a screenshot of Unity running the script and the inspector view of the giant X position. Thanks so much in advance![Unity number problem]1

\$\endgroup\$
1
\$\begingroup\$

Check the script in the inspector (click on the object that holds it). Looks like you changed the value there, which will override the default value due to how Unity's serialization system works. Thus, the default value specified in the script will be ignored.

\$\endgroup\$
  • 1
    \$\begingroup\$ You can remove the public modifier if you don't need to change it in the inspector and you want to avoid these kinds of issues in the future. \$\endgroup\$ – MichaelHouse Sep 5 '15 at 3:16
  • \$\begingroup\$ Followed your advice, looks like in the "Grid" object inspector, the value was somehow set to 10000. Removed public as well, works like a charm. Thanks to both of you. \$\endgroup\$ – Tyler Haddad Sep 5 '15 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.