1
\$\begingroup\$

Here is the UnityScript implementation of the method i use to generate a randomly tiled background, the problem i'm having relates to how to translate the call to the newTile method in c#, so far i've had no luck fiddling... can anyone point me in the correct direction?

Thanks

#pragma strict
import System.Collections.Generic;

var mapSizeX : int;
var mapSizeY : int;
var xOffset : float;
var yOffset : float;

var tilePrefab : GameObject;
var tilePrefab2 : GameObject;


var tiles : List.<Transform> = new List.<Transform>(); 


function Start () {
    var i : int = 0;
    var xIndex : int = 0;
    var yIndex : int = 0;
    xOffset = 2.69;
    yOffset = -1.97;

    while(yIndex < mapSizeY){
       xIndex = 0;
       while(xIndex < mapSizeX){

                var z = Random.Range(0, 5);   

                if (z > 2)
                {

                 var newTile : GameObject = Instantiate (tilePrefab, Vector3(xIndex*0.64 - (xOffset * (mapSizeX/10)), yIndex*-0.64 - (yOffset *  (mapSizeY/10)), 0), Quaternion.identity);
                 tiles.Add(newTile.transform);
                 newTile.transform.parent = transform;
                 newTile.transform.name = "tile_"+i;
                 i++;
                 xIndex++;

                 }

                 if (z < 2)
                {

                 var newTile2 : GameObject = Instantiate (tilePrefab2, Vector3(xIndex*0.64 - (xOffset * (mapSizeX/10)), yIndex*-0.64 - (yOffset *  (mapSizeY/10)), 0), Quaternion.identity);
                 tiles.Add(newTile2.transform);
                 newTile2.transform.parent = transform;
                 newTile2.transform.name = "Ztile_"+i;
                 i++;
                 xIndex++;

                }


       }

       yIndex++;
    }
}

C# Version [Fixed]

using UnityEngine;
using System.Collections;



public class LevelGen : MonoBehaviour {


    public int mapSizeX;
    public int mapSizeY;
    public float xOffset;
    public float yOffset;

    public GameObject tilePrefab;
    public GameObject tilePrefab2;

    int i;

    public System.Collections.Generic.List<Transform> tiles = new System.Collections.Generic.List<Transform>();

    // Use this for initialization
    void Start () {

        int i = 0;
        int xIndex = 0;
        int yIndex = 0;
        xOffset = 1.58f;
        yOffset = -1.156f;

        while (yIndex < mapSizeY)
        {
            xIndex = 0;
            while(xIndex < mapSizeX) {

                int z = Random.Range(0, 5);

                if (z > 5)
                {
                    GameObject newTile = (GameObject)Instantiate(tilePrefab, new Vector3(xIndex*0.64f - (xOffset * (mapSizeX/10.0f)), yIndex*-0.64f - (yOffset *  (mapSizeY/10.0f)), 0), Quaternion.identity);
                    tiles.Add(newTile.transform);
                    newTile.transform.parent = transform;
                    newTile.transform.name = "tile_"+i;
                    i++;
                    xIndex++;

                }

                if (z < 5)
                {                   
                    GameObject newTile2 = (GameObject)Instantiate(tilePrefab, new Vector3(xIndex*0.64f - (xOffset * (mapSizeX/10.0f)), yIndex*-0.64f - (yOffset *  (mapSizeY/10.0f)), 0), Quaternion.identity);
                    tiles.Add(newTile2.transform);
                    newTile2.transform.parent = transform;
                    newTile2.transform.name = "tile2_"+i;
                    i++;
                    xIndex++;               
                }

            }
            yIndex++;
        }

    }

    // Update is called once per frame
    void Update () {

    }
}
\$\endgroup\$
6
  • \$\begingroup\$ What errors are you actually getting? \$\endgroup\$ May 21 '14 at 8:40
  • \$\begingroup\$ I'm sorry i should have included that in the OP "Object reference not set to an instance of an object" Which just now i've managed to fix by casting it to type GameObject, the code now runs without errors but doesn't generate my terrain \$\endgroup\$
    – user46157
    May 21 '14 at 8:44
  • \$\begingroup\$ I think the problem may be me not understanding how to create the GameObjects repeatedly each iteration like i can in the .js \$\endgroup\$
    – user46157
    May 21 '14 at 8:49
  • \$\begingroup\$ Okay, i've fixed it after tinkering... Since i can't answer my own question, i've put comments on the edit :) \$\endgroup\$
    – user46157
    May 21 '14 at 9:27
  • 1
    \$\begingroup\$ Please follow site etiquette by doing as @blue suggests and accepting his/her answer. Thanks. \$\endgroup\$
    – Engineer
    May 21 '14 at 18:47
1
\$\begingroup\$

So we can mark it as answered I've reformatted the details you have put up. Can you please revert your changes in your question so future viewers can see what changes were made if they have the same issue.

Also I noticed you have a lot of logic redundancies (like your if (z>5) and if (z<5)) as they can be condensed into an if(){}else{} statement with a smaller random range, considering z is used for nothing but a Rand comparison.

C# Version:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class LevelGen : MonoBehaviour {

    public int mapSizeX;
    public int mapSizeY;

    public GameObject tilePrefab;
    public GameObject tilePrefab2;

    private float xOffset;
    private float yOffset;
    private int i;

    public List<Transform> tiles =
        new List<Transform>();

    // Use this for initialization
    void Start () 
    {    
        int i = 0;
        int xIndex = 0;
        int yIndex = 0;
        xOffset = 1.58f;
        yOffset = -1.156f;

        while (yIndex < mapSizeY)
        {
            xIndex = 0;
            while(xIndex < mapSizeX) 
            {    
                Vector3 tilePosition =
                    new Vector3(xIndex * 0.64f - (xOffset * (mapSizeX / 10.0f)),
                                yIndex * -0.64f - (yOffset *  (mapSizeY / 10.0f)),
                                0f);

                GameObject newTile =
                    (GameObject)Instantiate(tilePrefab,
                                            tilePosition,
                                            Quaternion.identity);

                int z = Random.Range(0, 3);
                if (z > 1)
                {
                    tiles.Add(newTile.transform);
                    newTile.transform.parent = transform;
                    newTile.transform.name = "tile_"+i;
                    i++;
                    xIndex++;    
                } 
                else
                {
                    tiles.Add(newTile.transform);
                    newTile.transform.parent = transform;
                    newTile.transform.name = "tile2_"+i;
                    i++;
                    xIndex++;               
                }
            }
            yIndex++;
        }
    }

    // Update is called once per frame
    void Update () 
    {    
    }
}
\$\endgroup\$

You must log in to answer this question.