Hi,
I’m creating a grid of objects that get placed in a defined size grid. In my script, Ive referenced the 60 objects, calling them tileObj_0 to tileObj_59. I have a 2D array, that numbers what object number should be in the corresponding grid place.
My code works fine, regarding placing the grid, if I tell it to just use a single tileObj_0 (or any other). What I need it to do is relabel the “tilObj_” to the corresponding number in the 2D array, so that it places the different scenery objects correctly, instead of just the same one everywhere.
Ive tried all manner of syntax, and ordering it.
the current line of code that works with a single object is:
GameObject.Instantiate (tileObj_0, tilePosition, Quaternion.Euler(Vector3.right));
It feels like it should be something like:
GameObject.Instantiate (“tileObj_(worldMap[19-tilePosY, tilePosX])”, tilePosition, Quaternion.Euler(Vector3.right));
But as you can see…I’m not getting it right… (I’m new to coding). I would really appreciate it if anyone could show me the correct syntax for doing this, as its got me pulling my hair out.
cheers in advance.
My script below:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
public class worldControl : MonoBehaviour {
public GameObject tileObj_0;
public GameObject tileObj_1;
public GameObject tileObj_2;
public GameObject tileObj_3;
public GameObject tileObj_4;
public GameObject tileObj_5;
public GameObject tileObj_6;
public GameObject tileObj_7;
public GameObject tileObj_8;
public GameObject tileObj_9;
public GameObject tileObj_10;
public GameObject tileObj_11;
public GameObject tileObj_12;
public GameObject tileObj_13;
public GameObject tileObj_14;
public GameObject tileObj_15;
public GameObject tileObj_16;
public GameObject tileObj_17;
public GameObject tileObj_18;
public GameObject tileObj_19;
public GameObject tileObj_20;
public GameObject tileObj_21;
public GameObject tileObj_22;
public GameObject tileObj_23;
public GameObject tileObj_24;
public GameObject tileObj_25;
public GameObject tileObj_26;
public GameObject tileObj_27;
public GameObject tileObj_28;
public GameObject tileObj_29;
public GameObject tileObj_30;
public GameObject tileObj_31;
public GameObject tileObj_32;
public GameObject tileObj_33;
public GameObject tileObj_34;
public GameObject tileObj_35;
public GameObject tileObj_36;
public GameObject tileObj_37;
public GameObject tileObj_38;
public GameObject tileObj_39;
public GameObject tileObj_40;
public GameObject tileObj_41;
public GameObject tileObj_42;
public GameObject tileObj_43;
public GameObject tileObj_44;
public GameObject tileObj_45;
public GameObject tileObj_46;
public GameObject tileObj_47;
public GameObject tileObj_48;
public GameObject tileObj_49;
public GameObject tileObj_50;
public GameObject tileObj_51;
public GameObject tileObj_52;
public GameObject tileObj_53;
public GameObject tileObj_54;
public GameObject tileObj_55;
public GameObject tileObj_56;
public GameObject tileObj_57;
public GameObject tileObj_58;
public GameObject tileObj_59;
public Vector2 mapSize;
public int tilePosX;
public int tilePosY;
// Use this for initialization
void Start () {
int[,] worldMap = new int[20, 20] {
{ 1,2,1,1,2,2,1,1,1,2,1,2,1,2,1,1,2,1,2,1 } ,
{ 2,15,14,43,51,14,15,6,14,15,32,12,4,16,44,6,0,34,5,2 } ,
{ 1,14,7,5,42,12,43,17,16,12,0,5,14,15,0,12,6,5,18,1 } ,
{ 1,43,5,47,4,7,40,15,25,26,44,0,15,16,14,16,4,0,3,1 } ,
{ 2,8,42,4,8,43,15,16,32,25,25,26,13,14,17,40,0,44,4,2 } ,
{ 1,51,0,7,42,4,7,51,26,25,0,0,3,16,15,14,13,0,34,1 } ,
{ 2,16,12,15,16,51,3,0,0,0,4,34,0,5,12,16,15,19,0,2 } ,
{ 1,15,14,32,12,3,21,20,21,0,4,0,5,44,0,4,18,26,18,1 } ,
{ 2,5,17,10,22,21,20,33,21,5,0,5,18,4,3,18,23,44,24,1 } ,
{ 1,15,10,0,4,20,3,21,0,32,15,13,3,5,0,24,33,23,22,2 } ,
{ 1,3,0,35,3,5,0,4,6,5,14,15,4,14,3,4,24,21,18,1 } ,
{ 2,18,5,0,34,0,19,3,16,44,15,17,16,13,19,10,4,23,3,2 } ,
{ 2,0,3,44,5,3,36,32,0,15,14,15,5,3,0,5,44,3,4,2 } ,
{ 1,4,0,0,3,32,0,3,17,16,40,13,3,34,0,3,4,0,10,1 } ,
{ 2,19,32,3,0,5,3,0,15,13,0,3,0,5,3,33,32,5,14,2 } ,
{ 1,26,23,19,23,3,0,14,0,3,23,0,5,10,0,45,0,15,4,1 } ,
{ 2,20,41,24,20,21,44,12,6,13,10,34,0,4,32,34,16,0,17,1 } ,
{ 1,22,21,23,33,20,0,13,10,8,33,13,44,5,11,12,17,15,14,2 } ,
{ 1,24,22,44,24,0,3,12,32,13,10,0,4,12,14,15,16,3,41,1 } ,
{ 2,1,2,1,1,2,1,2,1,1,2,1,2,1,1,2,1,2,1,2 }
} ;
//calls the code to generate the visual tiles
GenerateMap ();
}
// Update is called once per frame
void Update () {
}
public void GenerateMap() {
//drops a scenery sprite in rows defined by the mapSize, to create a grid
for (int x = 0; x < mapSize.x; x++) {
for (int y = 0; y < mapSize.y; y++) {
Vector3 tilePosition = new Vector3 (+x, 0, +y);
tilePosX = Convert.ToInt32 (tilePosition.x);
tilePosY = Convert.ToInt32 (tilePosition.y);
GameObject.Instantiate (tileObj_0, tilePosition, Quaternion.Euler(Vector3.right));
//tileObj_(worldMap[19-tilePosY, tilePosX])
}
}
}
}