- Home /
Accessing tiles in another class
Greetings,
My apologies if this question has been asked before. I've searched far and wide, and even though I do find answers, I either don't understand or they don't give me the answer that I need.
I'm keeping a grid of tiles that I am trying to refer back to, after creation. My first step, is this :
using UnityEngine;
using System.Collections;
public class copyPlane : MonoBehaviour {
public Transform myPrefab;
public int width;
public int height;
public float tileWidth;
public float tileHeight;
public float tileGap;
public Vector3 startLocation;
public TileList tiles;
// Use this for initialization
void Start () {
for (int iX = 0; iX< width; iX++)
{
for(int iY = 0; iY < height; iY++)
{
Vector3 loc = new Vector3();
loc.x = startLocation.x + ((tileWidth + tileGap) * iX);
loc.z = startLocation.z + ((tileHeight + tileGap) * iY);
loc.y = startLocation.y;
Object nPrefab = Instantiate (myPrefab, loc, myPrefab.rotation);
nPrefab.name = "tile_" + iX.ToString () + ";" + iY.ToString ();
tiles.AddTile (iX, iY, loc);
}
}
//Object nPrefab = Instantiate (myPrefab, new Vector3(6,1,20), myPrefab.rotation);
}
// Update is called once per frame
void Update () {
}
}
That works fine. Note the " public TileList tiles;". Then, in order to keep track of my tiles, I created this class :
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class TileList {
public List<TileElement> tiles;
// Use this for initialization
void Start ()
{
tiles = new List<TileElement> ();
}
public void AddTile(int x, int y, Vector3 location)
{
TileElement newTile = new TileElement (location, x, y);
tiles.Add (newTile);
}
// Update is called once per frame
void Update () {
}
}
public class TileElement
{
public Vector3 location;
public int x;
public int y;
public TileElement(Vector3 _location, int _x, int _y)
{
location = _location;
x = _x;
y = _y;
}
}
Super. Now that all populates on launch, and it makes me happy. However, now that I do have that list populates, and I want to move another object to tile X;Y ... How do I refer back to that list? I can't seem to access that class, as I would normally do in C#.
I think my biggest problem here is that I'm still thinking in normal c# terms, and don't have my mind wrapped around the Unity environment/concept.
Any help, pointers, death threats or girlfriend telephone numbers are welcome.
Regards, Maerdyn
EDIT : Properly encapsulated [code][/code]
This is technically a duplicate, but where are you accessing that list? Do you mean the TileList class of the generic list property inside it?
No, not from the same class. $$anonymous$$aybe my complete idea of the system is wrong here. What I want to do, is have a grid of tiles. Different tiles can have different objects on them. When another object comes on to the tile, I want that tile to trigger. I also want to be able to tell an object to move to a certain tile. In other words, I'll need to reference to a tile in order to get his vector. For that, I'll either need a global List's to refer to, or I must be able to pass that list between objects. For all that I know, I might need to redesign this from scratch. If so, I'm open for that option, if someone can steer me in the right direction. This is my first shot at designing my own game engine from scratch, so I do realize there's a huge possibility here that I'm doing it completely wrong.
Your answer

Follow this Question
Related Questions
Extending a Singleton base class (UJS) (?) 2 Answers
How to convert the retargeted animation back to Generic animation? 0 Answers
Why do I have to use a generic parameter when creating generic functions? 3 Answers
Passing a generic Array with unknow dimension 4 Answers
Are Unity distances that small? 1 Answer