1

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.

Habeeb
  • 7,601
  • 1
  • 30
  • 33
Dinu Adrian
  • 129
  • 2
  • 13
  • 3
    Objects are not stored within other objects. They are stored on the heap. An object could contain a *reference* to another object, but so could any number of other objects, or local variables, etc. So the premise behind your question is inherently flawed. If you need to find one object given another object, your only option is to define and set a reference within that object that points to it. So for example the child could have a field that points at the parent. That is something you’d have to add to the class yourself. – John Wu Nov 20 '19 at 01:29

3 Answers3

1

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.

DrkDeveloper
  • 939
  • 7
  • 17
1

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();
}

Quickest way to compare two generic lists for differences

Everts
  • 10,408
  • 2
  • 34
  • 45
1

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 Dictionarys for being able to go forth and back between GameObject and Tile like e.g.

    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 
            // e.g. 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.

derHugo
  • 83,094
  • 9
  • 75
  • 115