Create Unit Object but referencing an game object?

Hi,

I`m trying to understand this:

In this lecture, we created a “UnitActionSystem” script in which we create an object of type “Unit”, which “Unit” is the class we attached to the “Unit” game object. Why do we assign that game object to that “Unit” Object inside “UnitActionSystem” rather than the Class itself? I`m a bit confused and I would highly appreciate if someone can answer this question.

Thanks,
Norbert

I’m not quite sure what you mean. We are not creating any units in UnitActionSystem. We only have a reference to the selected Unit. If you can be a little more specific, I can answer your question better.

Oh, and welcome to the community :grin:


EDIT Are you referring to Hugo dragging the unit game object into the ‘Selected Unit’ field in the inspector? If so, this is just a nice perk of Unity. Unity sees that the unit gameobject has the Unit component and assigns the Unit component to the field, instead of the game object. If the unit game object did not have a Unit component, Unity would not allow you to assign the game object to the field because it cannot find a component that matches Unit

image
You can see here that the ‘Unit’ game object is assigned, but the type it assigned is Unit (the part in brackets is the type). It shows ‘Name (Type)’

2 Likes

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

Privacy & Terms