Hey guys I am trying to get a better under standing of objects.
Does Add component just add a class to a game object? what would be the difference in creating a new class / structure outside and then assigning it to a gameobject at runtime when I need it? (When a user clicks on a game object to see stats, using reference to class in list)
Lets say I want to create a world with 10000 characters and each one has for example a hungry meter. should I add a component to each one that subtracts itself by the time or have 1000 classes that subtract over time and assign the class when I need it?
I don’t understand what you’re saying, “adding a class to a game object” is not a real thing, what do you mean?
Unity uses a component based development, each object is a collection of components, and each component adds some new things to that object. This way if you want 2 unrelated things to be able to shoot, you just add a “Shooter” component to both.
A component in it’s basic form is a structure that holds some data, and the engine knows that if there’s a component of that type, the data it contains should be used in a specific way. In the most general case the data contained in a component can also be code, and Unity knows that each time it finds a component with code it might be able to call some defined functions (Awake, Start, Update, etc, etc).
When you Add a component your adding an instance of a Component to the list of components of a game object.
You can add components in the editor in a lot of ways, or you can add components in code when you need to (with the AddComponent method), but those components have to be defined in your project, an assets that contains the script for the component should already exists.
I don’t understand the last part about the 10000 characters, again, what do you mean by having “1000 classes that substract over time and assign the class when I need it”? You shouldn’t have 1000 classes that do the same, maybe you meant you have 1000 instances of a class. The only thing I can tell you is that you should have components inside gameobjects, having a list of components would be useless and I’m not even sure if that’s possible.