I am trying to make a list of "tiles". For this I defined a class named "Tile":
[System.Serializable]
class Tile
{
[SerializeField]
GameObject platform;
[SerializeField]
List<string> connectionNorth,
connectionSouth,
connectionWest,
connectionEast;
}
[SerializeField]
List<Tile> tiles = new List<Tile>();
Now...If I find the GameObject that is stored in the field platform
(does not matter how I find it, with a raycast, collision, trigger...etc.) How do I access it's parent class (the instance of the class that this specific gameobject is stored in, during runtime)?. By doing this I want to compare,let say connectionNorth
list with some other list I have.
You'll have to make a relation between gameobject and his parent.
Or GameObject have a "Parent" Property or field or you store the relation in another way.
Your Tile object has to be instantiated on a MonoBehaviour component, so you'd have something of the kind:
public class Platform : MonoBehaviour
{
public Tile tile;
}
then you can use platform.GetComponent<Platform>().tile
on the platform reference and access the Tile member.
You can compare tiles together, most likely with a static method:
public static bool CompareNorth(Tile a, Tile b)
{
return a.connectionNorth == b.connectionNorth;
}
It appears your connections are lists. I would think a tile only has up, down, left, right so there would be no need for a list. If it were to be a list anyway:
public static bool CompareNorth(Tile a, Tile b)
{
var firstNotSecond = a.connectionNorth.Except(b.connectionNorth).ToList();
var secondNotFirst = b.connectionNorth.Except(a.connectionNorth).ToList();
return !firstNotSecond.Any() && !secondNotFirst.Any();
}
It's not totally clear what you mean by parent
.
If you mean the parent GameObject
in the Scene Hierachy then you can simply access its Transform.parent
Transform parentTransform = platform.transform.parent;
If you rather mean to find out in which class your platform
is referenced.. you can't. The same GameObject
can be referenced in n
different other classes, there is no such thing like a parent
here. You would need a different data structure for this like maybe two Dictionary
s for being able to go forth and back between GameObject
and Tile
like eg
public class WhereYouUseIt: MonoBehaviour { public Dictionary<Tile, GameObject> GetGameObject = new Dictionary<Tile, GameObject>(); public Dictionary<GameObject, Tile> GetTile = new Dictionary<GameObject, Tile>(); public void AddTileGameObjectPair(Tile tile, GameObject obj) { // Ofcourse you probably would want some checks // eg if one of the references is already used as Key somewhere GetTile.Add(tile, obj); GetGameObject.Add(obj, tile); } }
In your description however you say you want to compare your lists.
Can't you simply do something like
if(connectionNorth.Contains(XYZ)) ...
Note
Btw Unity serialization does only work to a certain nesting level afaik. You might get trouble referencing a List<Tile>
within the class Tile
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.