ArgumentOutOfRangeException

So got this error because made everything like in the video.
And then followed to the lectures commit and found out the fix of this error.
But still i would like to understand, the source of this error.

So in the video we refactor our EnemyMover.
Here is the refactor from the video:

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

And here is the correct refactor from the commit:

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

So this issue completely messed up my understanding.
So in the video we changed our tags on the parent Path game object to tag “Path” and all the child tiles groupped inside to tag “Untagged”. And we even referred to Path game object as a parent. So now in the commit of the refactor is pretty much the same thing which we refactored. What is the point?

1 Like

Both solutions do work, but i still do not get it(

You can read about Argument out of range exception here:
https://learn.microsoft.com/en-us/dotnet/api/system.argumentoutofrangeexception?view=net-7.0

I don’t see anything in particular that would cause that in this script. I’d guess it’s from another.

In the first script, you’re going through all the children of a single game object that’s tagged with Path.

In the second script, you’re going through all the objects that are tagged with Path.

Thx for the response!

Just wanted to know why the pre refactoring solution is in the commit, not the refactored one.

/shrug

That’s a “don’t know” from me.

It seems that Gary forgot to make a commit or to push it. Or it might be that he edited the video after he completed the section and reworked the code. Have you already checked the history of the edited script on GitLab? Maybe there is a newer commit which includes the refactored version.

Actually, there are no newer commits(

Yes, there are.

However, you are right. This particular change is not included. Since Gary removes most lines from the FindPath method in one of the next commits, I assume he made the change in this video after completing this project. Unfortunately, it is not possible to edit a commit (or at least, one should not do that). Since he replaces the code in the next videos anyway, he probably decided to keep the old commit instead of messing around with the commit history.


Regarding your initial problem, were you able to solve the ArgumentOutOfRangeException error? Or do you still need help with this?

Thank you! I did manage with the issue!

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

Privacy & Terms