Dinu Adrian
Dinu Adrian

Reputation: 139

How do I access a class that is the parent of a variable which I know?

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.

Upvotes: 1

Views: 566

Answers (3)

derHugo
derHugo

Reputation: 90679

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.

Upvotes: 1

Everts
Everts

Reputation: 10701

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

Upvotes: 1

DrkDeveloper
DrkDeveloper

Reputation: 949

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.

Upvotes: 1

Related Questions