Foreach statement won't work on lecture 132

I have been following the unity course for a while now and have gotten to lecture 132 (The Dictionary Data Structure). I am getting an Error on saying (foreach statement cannot operate on variables of type ‘Waypoint’ because ‘Waypoint’ does not contain a public instance definition for ‘GetEnumerator’). If someone could help I would be really thankful, Here are some screenshots of my code.
Pathfinder


Pathfinder Error

CubeEditor

Waypoint

EnemyMovement

Hi Marxiplier,

Please note, it’s better to copy/paste your code and apply the code fencing characters, rather than using screenshots. Screenshots are ideal for displaying specific details from within a game engine editor or even error messages, but for code, they tend to be less readable, especially on mobile devices which can require extensive zooming and scrolling.

You also prevent those that may offer to help you the ability to copy/paste part of your code back to you with suggestions and/or corrections, meaning that they would need to type a potentially lengthy response. You will often find that people are more likely to respond to your questions if you make it as easy as possible for them to do so.

Please check line 15 in your Pathfinder script and compare it to Ben’s code. Can you spot the typo?

Hope this helps :slight_smile:


See also;

Sorry I can’t find it, if it’s possible could you help show me the difference because I can’t see any.
Here’s my code for Pathfinder.
‘’’
public class Pathfinder : MonoBehaviour
{
Dictionary<Vector2Int, Waypoint> grid = new Dictionary<Vector2Int, Waypoint>();
void Start()
{
LoadBlocks();
}

private void LoadBlocks()
{
    var waypoints = FindObjectOfType<Waypoint>();
    foreach (Waypoint waypoint in waypoints)
    {
        var gridPos = waypoint.GetGridPos();
        bool isOverlapping = grid.ContainsKey(gridPos);
        if (isOverlapping)
        {
            Debug.LogWarning("Skipping overlapping block" + waypoint);
        }
        else
        {
            grid.Add(gridPos, waypoint);
        }
    }
    print("Loaded " + grid.Count + " blocks");
}

}
‘’’
Please just tell me if you need a better look at my other scripts.

FindObjectOfType must be changed to FindObjectsOfType.


See also:

Thanks.
It’s fixed now.

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

Privacy & Terms