List Class Variable?

There’s a few new concepts here skimmed over.

[SerializeField] List Path = new List();

We seem to be creating a list with reference to the Waypoint class, and then assigning that to a variable, but no explanation given about how this really works? I feel like that is quite a new substantial step that needs a bit more explanation?

Hi,

Welcome to our community! :slight_smile:

What exactly would you like to know?

Most parts of Unity are not open-source. If you want to know how [SerializeField] works behind the scenes, I’m afraid we do not know that either. All we know is that the [SerializeField] attribute exposes a field in the Inspector in Unity.

1 Like

When we define List<Waypoint> we specify that we want a List that can hold Waypoint items. The generic version of List let’s us define what type of data we’d like in there. It’s similar to having an array of Waypoint

Waypoint[] pathArray = new Waypoint[5];
List<Waypoint> pathList = new List<Waypoint>();

Both of these will hold Waypoint items. The List is a little easier to work with than arrays, but it’s essentially the same thing. I’m not quite sure what you need more explanation on.

My apologies, I pasted in the wrong code above. Thank you for your help.
I meant to paste; List Path = new List ( );
So I generally understand the basics of lists and arrays.

  • But I don’t fully understand why we refer to the ‘Waypoint’ blank script we created.
    Would you mind in plain english explaining this line of code in a clear general English sentence?
    Thanks again

My apologies, I pasted in the wrong code above. Thank you for your help.
I meant to paste;
List Path = new List ( );

So I generally understand the basics of lists and arrays. And I get what the code does, however
I don’t fully understand why we refer to the ‘Waypoint’ blank script we created and how it works.
Would you mind in plain english explaining this line of code in a clear general English sentence?
Thanks again for your help

It might help if you explain what you think you do understand and what you suspect you don’t understand. What you’ve asked doesn’t make a lot of sense to me as I’d answer, “It’s a list of waypoints (GameObjects) so when you need to use them, you can use the waypoints that are on the list. Typically it would be used to get the transform.position of each waypoint.”

Not sure if something like that would answer your question.

Dang when I paste code it takes out half of it??
So do you mid explaining why we refer to the blank waypoint script and why we had to create that blank script and how ti is being implemented in the list?

Thank you

@S4V3D8YGR4C3 See this post on formatting code in the forum

Using blank scripts is usually a way to restrict the objects that can be added to that list. If we didn’t create the script, we’d have to make the list of type GameObject or Transform which would allow us to add any object to the list. By making a Waypoint script and attaching it game objects, we are restricting which GameObjects can be added to this list because only those with the script will now be allowed to be dropped in the inspector.

1 Like

You have to set the correct quotation for it. Highlight the code and press the button that looks like </> and parts of the code shouldn’t disappear when you post/reply.

eg:
List<Path> path = new List<Path>( );

Bix explained why you refer to the blank waypoint script.

It’s not being implemented at all. It’s only data that’s currently stored in a list. You’ll need to implement it later for it to be anything more than just a list of waypoints… sitting there… doing not much.

Usually you’d have code on something like a mover script that takes transform.position data from waypoints in that list and moves an object between those waypoints.

1 Like

Hello @S4V3D8YGR4C3,

@bixarrio has good answers and I’ll just add a bit to it in case it helps:

The blank Waypoint script was created and added as a component to the Tile prefab. This makes the Tile prefab (and any instances of it) of type Waypoint. The list that we create in the EnemyMover script is set up to be a list of items and we designate those items to be of type Waypoint. Thus, GameObjects that have the Waypoint script attached are able to be added to the list.

This is mentioned quickly in the tutorial video (around the 3:40 mark), but he says a similar result could have been achieved using tags. I think it is helpful to think of our script like a tag. We created a name for our tag (Waypoint), assigned it to our desired GameObject (in the form of a component on the Tile prefab), and then made our list capable of containing anything with that tag (anything of type Waypoint).

Hope that helps!

2 Likes

Thank you for explaining that-that’s a big help.

Thank you - I appreciate your time and explanation. That’s a big help

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

Privacy & Terms