Just to clarify

Anyone knows what is the right version of the FindPath() method? the one in the repo or the one in the lecture? I’ve tryed to seek in the comments below but it is still not clear which one is latest version.

this is the version in the repo:

 void FindPath()
    {
        path.Clear();

        GameObject[] tiles = GameObject.FindGameObjectsWithTag("Path");

        foreach(GameObject tile in tiles) 
        {
            Waypoint waypoint = tile.GetComponent<Waypoint>();

            if(waypoint != null)
            {
                path.Add(waypoint);
            }
        }
    }

and thiis is the one in the lecture:

 void FindPath()
    {
        path.Clear();

        GameObject parent = GameObject.FindGameObjectWithTag("Path");

        foreach(Transform child in parent.transform) 
        {
            Waypoint waypoint = child.GetComponent<Waypoint>();

            if(waypoint != null)
            {
                path.Add(waypoint);
            }
        }
    }

The “parent” version is supposed to get the childs in the parent that is tagged with “Path” withoud the risk of getting the child in random order and possibly messing up the pathing,

Iis the first one that is in the repo doing the same thing but without using the parent trick?

Or is just the one in the lecture the one to follow?

Probably i’ve overthinking this as i always do, and this methods will be replaced anyway further in the project, but man… ADHD and OCD combined are a pain in the butt.

Hi Zan,

That’s indeed odd. I’m glad you asked. :face_with_monocle:

The difference between the two versions is the following:

GameObject[] tiles = GameObject.FindGameObjectsWithTag("Path"); indicates that there are multiple game objects with the ‘Path’ tag in the scene. And since the code iterates over them, those returned objects are apparently supposed to be the waypoints.

In the version from the video, we look for one game object with the ‘Path’ tag, and iterate over the children, which are obviously supposed to be the waypoints.

The order of the waypoint objects is irrelevant because we are going to check positions/coordinates, not the waypoint objects themselves. In this respect, both versions are valid.

With this information in mind, which version would you prefer and why?

Here is my reasoning:

I prefer the version in the video because looking for multiple objects is less performant than looking for only one. Also, it is easier to inadvertendly not add ‘Path’ to one of the game objects than to have a parent with the ‘Path’ tag and a bunch of children.

Did this help? :slight_smile:


See also:

1 Like

I’m liking the second the most too, for similar reasons, even thou the first could be easyer to came up as an idea, because iterating through an array make more sense logically, than just take the child transform.position.
Well seems the question is sorted now…it’s the one in the lecture the new version to follow, as Gary was referring.
thanks :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms