Small questions about the lecture

Hey,

I would just like to really understand some things so I figured it might be worth throwing a question out here to see if someone else picked it up:)

  1. Below the serializing of the Waypoint list. What are we actually doing here? Why do we need to specify that it’s a . This is not what we tend to do with things like GameObjects. Why do we need to do it now?

  [SerializeField] List<Waypoint> path = new List<Waypoint>();

Below I fully get that we are getting waypoints from path but why is there two Waypoints in this instance?

Why isn’t there just:

foreach (waypoint in path).

foreach (Waypoint waypoint in path)

Hopefully everyone has a good evening / day / morning depending on where you are

//Martin

Hi Murten,

Thank you for your questions. :slight_smile:

  1. Just to be sure we are talking about the same: Are your questions referring to the following line of code?
[SerializeField] List<Waypoint> path = new List<Waypoint>();

If so, we call the constructor to create a List object. We do not do that for things like GameObject objects because Unity does not allow us to call the constructor of the GameObject class. List is a “normal” C# class, not a Unity class.

Since we use [SerializeField] here, we actually do not need to call the constructor of the List method because the path variable appears in the Inspector in the Unity engine. I think Unity creates a List object for us. However, to be on the safe side, it’s a good idea to call the constructor anyway.

  1. There are no two waypoints in the foreach header if you are referring to Waypoint waypoint only. Waypoint waypoint is a variable declaration. That variable is required because the foreach program assigns the current object of the respective iteration step to it. The name of the variable does not matter as long as it is unambiguous. You could declare it as Waypoint abc, and the foreach loop would still work. path is the second Waypoint variable.

Did this clear it up for you? :slight_smile:


See also:

It absolutely did clear things up for me. Thank you very much for taking the time and answering!

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