Object naming woes...(syntax issue?)

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])
            }
        }
    }
}

Please don’t pull your hair out… All will work out :slight_smile:
So, you have a bunch of objects, it’s often better to use an array or List. Just pick one or the other, don’t fuss about it too much, especially since it’s size is not changing, there is a negligible difference for your script.
So, the array/list would be to hold all your game objects. (eg: tileObj_37, and so on).
When you go to instantiate the object, use the appropriate index in the list.
ListVariable[indexHereAsInteger] :slight_smile:

Oh, and about your loop:
you needn’t do: tilePosX = Convert … etc
Since your Vector3 is made just by the x and y of the loops, just assign those values to tilePosX/Y (if you’re going to even use them) – right now, you’re not using them, … in fact I don’t see how you could use them anywhere where x/y by themselves wouldn’t already work, anyways. :slight_smile:

Sounds promising, but I’m just not getting it…

Would it be possible to edit my script a bit, to show what you mean?

Oh wait a second… I think I misread your post a little bit!

That array you have says which tiles should spawn there… Okay, so you make a list of gameobjects and then you just instantiate based on the value in your 2d array.

List <GameObject> TileObjs = new List<GameObject>(); // this replaces your 0-59, fill them in in the inspector.

// in your loop
Instantiate(TileObjs[worldMap[x,y]] // etc..

You could copy your script,make these changes & test it out. :slight_smile:

ok… so I got most of the way through organising it the way you recommended, with the List.

It works fine if I instantiate a single object,

Instantiate (TileObjs[12]// etc

but collapses on its arse when I put in the:

(TileObjs[worldMap[x,y]]

So kind of back where I was at the start again… but just organised differently now :slight_smile:

Ive run test prints for the worldMap array to make sure its actually there, and they come back correct… It just errors out with the following and doesn’t place any of the tiles.

NullReferenceException: Object reference not set to an instance of an object
worldControl.GenerateMap () (at Assets/worldControl.cs:95) //this is the line with the instantiate
worldControl.Start () (at Assets/worldControl.cs:74) //this is the line that triggers the GenerateMap ()

Okay, so the world map array is working, the list is working but you cannot index from the worldmap to the list.
This seems weird, maybe something simple. What if you do like:

 int tilenum = worldMap[x,y]; Instantiate(TileObjs[tilenum]);

And if/when you hit an issue, debug and see what values you’re getting there?

ok… did that. Still no luck.

if I set:
int tilenum = 15; (or any other number 0-59)

it works again… but obviously sets all the placed objects to that version.

if I do:
int tilenum = worldMap[x,y];

it does the NullReference Exception again.

so yup, cant seem to index from the worldMap.

but a number of:
print (worldMap [,]);

all return the correct values from the array, showing that its working, and there… :frowning:

I really appreciate the time and effort you’ve put into trying to help me with this Methos5k, its turning into a bit of a nightmare, so I wouldn’t blame you if you cut and run :slight_smile:

When it does null reference exception with : int tilenum = worldMap[x,y] what’s the value of tilenum there, if you log it?

Makes no sense that you can print the correct number and index some random spots. But when you do them together, they’re not working lol

Just to double check, are ya sure that all 60 tile objects are part of the list in the inspector? One (or more) aren’t missing/empty?

checked all the 60 objects in the inspector are correct and there…

not sure how I log the value of int tilenum=worldMap[x,y] when it does the null reference exception

Well, I don’t want to give up on you. Do you think it’s possible that you can put just this 1 script with the tiles in a zip & post me a dropbox link? :slight_smile: I’m sure we can work this out… lol

It shouldn’t be possible to get a null reference on worldMap[x,y]. Maybe for worldMap itself, if you tried to reference it before it was created/initialized. Otherwise, it should be returning an integer (or an out of range, if that happened). integers cannot be null… but it just seemed easier to ask for the script … Pretty sure I can sort it out for you. :wink:

yup…will put the full project up on dropbox and pm you a link

Cool :slight_smile: If it’s not big I don’t mind if it’s the whole project. If it’s huge, I would appreciate it if you just sent the tiles + that 1 script. lol. But whatever… :slight_smile:

link sent. total project is 57mb, or you can just grab the bits

Found the problem in about 10 seconds.
After I downloaded all the tiles & 1 script lol

cool… is it a slap myself in the face kind of problem?

This is the change I meant to Instantiate:

Instantiate(tileList[worldMap[x, y]], new Vector3(x,0,y), Quaternion.Euler(Vector3.right));

tileposition wasn’t really needed since it wasn’t doing anything.
That being said, if you need that later, then the variable makes sense (if you’re using it again in that loop, I mean).

your variables “tilePosX/Y” I have no idea what those are for. I removed them in that area. If there is something you had in mind to use those with, that’s another story… I just couldn’t tell with the limited stuff I was looking at.

Finally, the missing jigsaw piece… Your variables (arrays) worldMap and areaMap : you redeclared local copies of them!! that’s why it didn’t work. In Start(), change this:
int[,] worldMap = new int[20, 20] {
to this worldMap = new int[20,20] { // etc
Same for areaMap I think it’s called :slight_smile:

I’m sorry that I didn’t notice that sooner. When I scroll up this page, that’s actually visible in a previous post that you made. Anyways, … hopefully the main thing is that it should be fixed now lol

You did it… :slight_smile:

You got me working as intended. I don’t know how to thank you.

Ive spent days trying to get that working. Like I said, I’m new to coding, so making a bit of a hash of it at times :slight_smile:

You sir, are a superstar. :slight_smile:

I think some of those unused variables are from the many different failed attempts at getting it working… all deleted now!